Multiplication in PHP
I have to do a calculation, here is a simplification of what I am trying to achieve:
$box['dim1'] = 1;
$box['dim2'] = 1;
$box['dim3'] = 1;
$volume = (($box["dim1"]/100) * ($box["dim2"]/100) * ($box["dim3"]/100));
echo $volume;
In this case I see:
1.0E-6
Rather than 0.000001 as I would expect.
This is a rather extreme example and dim1, dim2 and dim3 are unlikely to ever all equal 1 but I need the value of $volume to be a number as this is passed to a 3开发者_JAVA百科rd party API and they don't like 1.0E-6.
Is there anyway I can get $volume to equal 0.000001 and not 1.0E-6?
Scientific notation is PHP's default for printing extremely large, or extremely small, numbers as strings.
Use printf()
to print your float in decimal format:
printf('%f', $volume);
If you just want to format the number use number_format. If you provide a second argument it will go to that many places.
$box['dim1'] = 1.0;
$box['dim2'] = 1.0;
$box['dim3'] = 1.0;
$volume = (($box["dim1"]/100.0) * ($box["dim2"]/100.0) * ($box["dim3"]/100.0));
echo number_format($volume, 6);
[1] http://us.php.net/number_format
0.000001 is 1.0E-6, it's just it's moving it to scientific expnential for to display it (imagine displaying really small numbers other ways frequently).
Now, you could use printf() function instead and then specify the display of the data how you see fit.
0.000001 is 1.0E-6.
Check this out:
http://en.wikipedia.org/wiki/Scientific_notation
When working with floating point values, if you need to output it's value, you should always format the value.
In PHP, you can use the number_format function :
echo number_format($volume, 6);
精彩评论