PHP 浮点数 2 位小数:.00
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12706519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP float with 2 decimal places: .00
提问by StackOverflowNewbie
When I do this typecasting:
当我进行这种类型转换时:
(float) '0.00';
I get 0. How do I get 0.00and still have the data type as a float?
我明白了0。我如何获得0.00并仍然将数据类型作为浮点数?
回答by Denys Séguret
A float isn't have 0or 0.00: those are different string representationsof the internal (IEEE754) binary format but the float is the same.
浮点数没有0或0.00:这些是内部(IEEE754)二进制格式的不同字符串表示,但浮点数是相同的。
If you want to express your float as "0.00", you need to format it in a string, using number_format:
如果要将浮点数表示为“0.00”,则需要使用number_format将其格式化为字符串:
$numberAsString = number_format($numberAsFloat, 2);
回答by Tim B.
As far as i know there is no solution for PHP to fix this. All other (above and below) answers given in this thread are nonsense.
据我所知,PHP 没有解决方案可以解决这个问题。此线程中给出的所有其他(以上和以下)答案都是无稽之谈。
The number_format function returns a string as result as written in PHP.net's own specification.
number_format 函数返回一个字符串作为 PHP.net 自己的规范中所写的结果。
Functions like floatval/doubleval do return integers if you give as value 3.00 .
如果你给出值 3.00 ,像 floatval/doubleval 这样的函数会返回整数。
If you do typejuggling then you will get an integer as result.
如果您进行类型杂耍,那么您将得到一个整数作为结果。
If you use round() then you will get an integer as result.
如果你使用 round() 那么你会得到一个整数作为结果。
The only possible solution that i can think of is using your database for type conversion to float. MySQL for example:
我能想到的唯一可能的解决方案是使用您的数据库进行类型转换为浮动。以 MySQL 为例:
SELECT CAST('3.00' AS DECIMAL) AS realFloatValue;
Execute this using an abstraction layer which returns floats instead of strings and there you go.
使用抽象层执行此操作,该抽象层返回浮点数而不是字符串,然后就可以了。
JSON output modification
JSON 输出修改
If you are looking for a solution to fix your JSON output to hold 2 decimals then you can probably use post-formatting like in the code below:
如果您正在寻找一种解决方案来修复您的 JSON 输出以保留 2 个小数,那么您可以像下面的代码一样使用后格式化:
// PHP AJAX Controller
// some code here
// transform to json and then convert string to float with 2 decimals
$output = array('x' => 'y', 'price' => '0.00');
$json = json_encode($output);
$json = str_replace('"price":"'.$output['price'].'"', '"price":'.$output['price'].'', $json);
// output to browser / client
print $json;
exit();
Returns to client/browser:
返回到客户端/浏览器:
{"x":"y","price":0.00}
回答by Aurelio De Rosa
0.00 is actually 0. If you need to have the 0.00 when you echo, simply use number_formatthis way:
0.00 实际上是 0。如果您在回显时需要有 0.00,只需这样使用number_format:
number_format($number, 2);
回答by Bart Friederichs
Use the number_format()function to change how a number is displayed. It will return a string, the type of the original variable is unaffected.
使用该number_format()功能可以更改数字的显示方式。它将返回 a string,原始变量的类型不受影响。
回答by mareks
You can show float numbers
您可以显示浮点数
- with a certain number of decimals
- with a certain format (localised)
- 有一定的小数位数
- 具有特定格式(本地化)
i.e.
IE
$myNonFormatedFloat = 5678.9
$myGermanNumber = number_format($myNonFormatedFloat, 2, ',', '.'); // -> 5.678,90
$myAngloSaxonianNumber = number_format($myNonFormatedFloat, 2, '.', ','); // -> 5,678.90
Note that, the
请注意,该
1st argument is the float number you would like to format
第一个参数是您要格式化的浮点数
2nd argument is the number of decimals
第二个参数是小数位数
3rd argument is the character used to visually separate the decimals
第三个参数是用于在视觉上分隔小数的字符
4th argument is the character used to visually separate thousands
第四个参数是用于在视觉上分隔数千个的字符
回答by Amrish Prajapati
you can try this,it will work for you
你可以试试这个,它对你有用
number_format(0.00, 2)
回答by Dimuthu Nilanka
回答by Sr. PHP Programmer Team Lead
When we format any float value, that means we are changing its data type to string. So when we apply the formatting on any amount/float value then it will set with all possible notations like dot, comma, etc. For example
当我们格式化任何浮点值时,这意味着我们将其数据类型更改为字符串。因此,当我们对任何数量/浮点值应用格式时,它将使用所有可能的符号进行设置,例如点、逗号等。例如
(float)0.00 => (string)'0.00',
(float)10000.56 => (string) '10,000.56'
(float)5000000.20=> (string) '5,000,000.20'
So, logically it's not possible to keep the float datatype after formatting.
因此,从逻辑上讲,格式化后不可能保留浮点数据类型。
回答by Love Kumar
try this
尝试这个
$result = number_format($FloatNumber, 2);
回答by jewelhuq
You can use round function
您可以使用圆形功能
round("10.221",2);
Will return 10.22
将返回 10.22

