Asked  2 Years ago    Answers:  5   Viewed   463 times

When i make the following multiplication in PHP:

$ret = 1.0 * 0.000000001;

i get the result: 1.0E-9

I want to convert this result into the normal decimal notation, how can i do this?

sprintf('%f',$ret) doesn't work, it returns 0.000000. Overflow?



sprintf('%f',$ret) doesn't work, it returns 0.000000. Overflow?

sprintf works, however you miss some point here.

0.000000 is not overflow. It's just that sprintf for the %f modifier uses 6 digits per default. Also please take care that %f is locale aware, %F is probably better suited.

You might want to use more digits, e.g. let's say 4 000 000 (four million):

$ php -r "printf('%.4000000F', 1*0.000000001);"

Notice: printf(): Requested precision of 4000000 digits was truncated to PHP maximum of 53 digits in Command line code on line 1

Call Stack:
    0.0001     319080   1. {main}() Command line code:0
    0.0001     319200   2. printf() Command line code:1


As this example shows, there is not only a common value (6 digits) but also a maximum (probably depended on the computer system PHP executes on), here truncated to 53 digits in my case as the warning shows.

Because of your question I'd say you want to display:


Which are nine digits, so you need to write it that way:


However, you might want to do this:

rtrim(sprintf('%.20F', $ret), '0');

which will remove zeroes from the right afterwards:


Hope this is helpful.

Tuesday, August 30, 2022

So apparently printf is not meant to be echoed. At all.

Simply changing the instances of printf to sprintf fixed that problem.

Furthermore, removing the echo, and just running the command as printf("%.02f", 10) does, in fact, print 10.00, however, it should be noted that you cannot append strings to printf like you can with echoing.

If you ask me, PHP should've thrown a syntax error, unexpected T_FUNCTION or something, but I digress.

Tuesday, August 23, 2022

You can try to use number_format it won't be perfect since you have to provide number of decimals, but should help.

echo number_format(8-6.4, 50);


echo number_format(2.3*100, 50);


Edit: As the number of decimal places is varying (this also depends on the system used) the following might be useful - gets the full number for sure and removes trailing zeros:

echo rtrim(number_format(1.0/3.432, 100),0);


Tuesday, December 27, 2022

Finally I do it by hand:

public static String parseToCientificNotation(double value) {
        int cont = 0;
        java.text.DecimalFormat DECIMAL_FORMATER = new java.text.DecimalFormat("0.##");
        while (((int) value) != 0) {
            value /= 10;
        return DECIMAL_FORMATER.format(value).replace(",", ".") + " x10^ -" + cont;
Monday, October 31, 2022

You could install a third-party library that handles that. For example it looks like QPFloat gives you a new struct called System.Quadruple which overrides ToString, so you could try that.

(I wonder when .NET will support something like System.Quadruple.)

Saturday, November 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages