Reputation: 35
I have to store numbers with many decimal places. For example: 0.000000054
If I store it in a column float data type the value is saved as: 5.4E-8
This value has to be displayed in php template with original format, how can I do it?
Would it be correct to store decimals into varchar?
Upvotes: 1
Views: 1058
Reputation: 3282
Here it comes:
$number = 0.000000054;
echo sprintf('%.9f', $number);
// ^---- this number indicates how many digits are shown //
Use this for displaying only. Its a normal thing programming-languages turn large numbers into potential-format. You can calculate with them just as with any other number.
Upvotes: 0
Reputation: 11832
0.000000054
and 5.4E-8
are both a string representation of the same float value. When converting to a string PHP just prefers to show the latter one, because it uses less characters. This way of numbering allows for much smaller/bigger numbers by storing them in the same amount of memory.
To force the output into the first type of string, use number_format()
number_format($myFloat, 9);
Upvotes: 1