PHP how to show all decimal places? -
This may be a stupid question but I have searched repeatedly without searching for any results.
< P> So, what I want, without knowing how many decimal places it will be, that is to show all decimal places without any number. View this short code: $ arrayTest = array (0.123456789, 0.0123456789); Forex Currency ($ array $ as output) {$ newNumber = $ Output / 1000; Echo $ newNumber; Echo "& lt; br & gt;"; }
This gives this output:
0.000123456789 1.23456789 E-5
Now, I have 'number_format' Tried to use ', but I do not think it is a good solution, it determines the exact amount of decimal places, and I do not know the quantity of decimal place for every number. Look at the code given below:
$ arrayTest = array (0.123456789, 0.0123456789); Forex Currency ($ array $ as output) {$ newNumber = $ Output / 1000; Resonance number_format ($ new number, 13); Echo "& lt; br & gt;"; }
This output returns:
0.000023456789
Now, as you can see that an additional 0, in the first number, because the number-form has 13 decimal places.
I can get some problems in this direction. Is PHP.ini a setting that determines the quantity of decades? Thank you very much in advance! (And feel free to ask if you have any other questions)
To answer this "impossible" "The question is properly - because the binary float representation of the decimal number is approximate:
The closest you can come to you is to write yourself a routine that looks at the decimal representation of a number, and it is" exact " value; Once the difference becomes "small enough for your purpose," you stop adding more points.
This routine can then return "correct number of digits" as a string.
Example:
& lt ;? Php $ a = 1.234567890; $ B = 0.123456789; Echo return string ($ a) "\ N"; Echo return string ($ B). "\ N"; Returns the value in the form of $ a string with enough digits for the function return string ($ a) {// "to be exact" - that is, the value matches the given value for 1e-10/1 / / a The limit is to cope with the unexpected input / 10 points and prevent an infinite loop $ conv_a = 0; $ Points = 0; While (abdomen ($ a - $ conv_a) & gt; 1e-10) {$ numeral = $ numeral + 1; $ Conv_a = 0 + number-format ($ a, $ numeral); If ($ digits> 10) $ conv_a = $ a; } Return $ conv_a; }? & Gt;
In the above code, I have arbitrarily assumed that when corrected within 1E,
1.23456789 0.123456789
< P> 10 was pretty good, obviously you can change this condition to the number that are appropriate, and you can make it an alternative argument for your function too. Play with it - if it is not clear then ask questions.
Comments
Post a Comment