Asked  7 Months ago    Answers:  5   Viewed   69 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.

Wednesday, March 31, 2021
answered 7 Months ago

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.

Wednesday, March 31, 2021
answered 7 Months ago

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);


Saturday, May 29, 2021
answered 5 Months ago

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;
Sunday, June 6, 2021
answered 5 Months ago

You could use colorbar's format parameter:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker

img = np.random.randn(300,300)
myplot = plt.imshow(img)

def fmt(x, pos):
    a, b = '{:.2e}'.format(x).split('e')
    b = int(b)
    return r'${} times 10^{{{}}}$'.format(a, b)

plt.colorbar(myplot, format=ticker.FuncFormatter(fmt))

enter image description here

Sunday, July 11, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :