Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php- floating point number shown in exponential form

Tags:

php

Could anyone please tell me why this happens,

$a  = 0.000022

echo $a // 2.2E-5

What I want to see is 0.000022 not 2.2E-5

like image 759
robert Avatar asked Jul 07 '11 08:07

robert


People also ask

Is a number with a decimal point or a number in exponential form PHP?

PHP Floats A float is a number with a decimal point or a number in exponential form. 2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats. The float data type can commonly store a value up to 1.7976931348623E+308 (platform dependent), and have a maximum precision of 14 digits.

What is E in floating point number?

Because superscripted exponents like 107 cannot always be conveniently displayed, the letter E or e is often used to represent times ten raised to the power of (which would be written as "x 10b") and is followed by the value of the exponent.

How do you check if a number is int or float in PHP?

The is_float() function checks whether a variable is of type float or not. This function returns true (1) if the variable is of type float, otherwise it returns false.


2 Answers

The exponential form is the internal one use by every (?) programming language (at least CPUs "sees" floats this way). Use sprintf() to format the output

echo sprintf('%f', $a);
// or (if you want to limit the number of fractional digits to lets say 6
echo sprintf('%.6f', $a);

See Manual: sprintf() about more information about the format parameter.

like image 148
KingCrunch Avatar answered Nov 07 '22 16:11

KingCrunch


use number_format() function

echo number_format($a,6,'.',',');

the result would be 0.000022

like image 35
Paritosh Pandey Avatar answered Nov 07 '22 17:11

Paritosh Pandey