[英]PHP show float number in string
I want to show a float number in php string.我想在 php 字符串中显示一个浮点数。
something like:就像是:
My float number is: 0.00003485
But when I am using echo I see it like this:但是当我使用 echo 时,我看到它是这样的:
My float number is: 3.485E-5
Also, I know I can use printf("%.10f",$float)
code, but I need to use it in lots of places in my string, So I can't use printf
.另外,我知道我可以使用printf("%.10f",$float)
代码,但是我需要在字符串的很多地方使用它,所以我不能使用printf
。
What code should I use on a string, to show floats as they are?我应该在字符串上使用什么代码来显示浮点数? I don't want that shorted number (3.485E-5).我不想要那个短路号码(3.485E-5)。
At some condition i am prefer declaration float number as string:在某些情况下,我更喜欢将浮点数声明为字符串:
MyFloatNumber = "0.0000000000000000000000003485"; MyFloatNumber = "0.0000000000000000000000003485";
so,所以,
echo MyFlatNumber;回声 MyFlatNumber; //0.0000000000000000000000003485 //0.0000000000000000000000003485
I know I can use
printf("%.10f",$float)
code, but I need to use it in lots of places in my string, So I can't use printf.我知道我可以使用printf("%.10f",$float)
代码,但是我需要在字符串中的很多地方使用它,所以我不能使用 printf。
I have to respectfully disagree with that:我必须恭敬地不同意这一点:
printf('My float number is: %1$.8f (again: %1$.8f; once more: %1$.8f)', 0.00003485);
Or, if you need it in a string:或者,如果您需要在字符串中使用它:
$foo = sprintf('My float number is: %1$.8f (again: %1$.8f; once more: %1$.8f)', 0.00003485);
Of course, if you mean it's against project style guidelines or company policy, that's another story.当然,如果你的意思是它违反了项目风格指南或公司政策,那就是另一回事了。
What code should I use on a string, to show floats as they are?我应该在字符串上使用什么代码来显示浮点数?
Really?真的吗? Floats in PHP are a 64-bit IEEE 754 double precision format bitmap. PHP 中的浮点数是 64 位IEEE 754双精度格式位图。 According to this online converter , number 0.00003485
would be:根据这个在线转换器,数字0.00003485
将是:
0010010001010111110011100001110100101110111001001111
Of course, they aren't actual ones and zeroes but different voltage levels ;-)当然,它们不是实际的 1 和 0,而是不同的电压水平 ;-)
You can write a function to handle this in any way you want.您可以编写一个函数来以任何您想要的方式处理此问题。 Why not?为什么不?
function fval($f, $precision = 9) {
return number_format($f, $precision);
}
Then any place you need to format, call it out:然后任何需要格式化的地方,都叫出来:
$f = 3.485E-5;
echo fval($f); // this will print: 0.000003485
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.