PHP float with 2 decimal places: .00

Php

Php Problem Overview


When I do this typecasting:

(float) '0.00';

I get 0. How do I get 0.00 and still have the data type as a float?

Php Solutions


Solution 1 - Php

A float doesn't have 0 or 0.00 : those are different string representations of the internal (IEEE754) binary format but the float is the same.

If you want to express your float as "0.00", you need to format it in a string, using number_format :

$numberAsString = number_format($numberAsFloat, 2);

Solution 2 - Php

As far as i know there is no solution for PHP to fix this. All other (above and below) answers given in this thread are nonsense.

The number_format function returns a string as result as written in PHP.net's own specification.

Functions like floatval/doubleval do return integers if you give as value 3.00 .

If you do typejuggling then you will get an integer as result.

If you use round() then you will get an integer as result.

The only possible solution that i can think of is using your database for type conversion to float. MySQL for example:

SELECT CAST('3.00' AS DECIMAL) AS realFloatValue;

Execute this using an abstraction layer which returns floats instead of strings and there you go.


JSON output modification

If you are looking for a solution to fix your JSON output to hold 2 decimals then you can probably use post-formatting like in the code below:

// PHP AJAX Controller

// some code here

// transform to json and then convert string to float with 2 decimals
$output = array('x' => 'y', 'price' => '0.00');
$json = json_encode($output);
$json = str_replace('"price":"'.$output['price'].'"', '"price":'.$output['price'].'', $json);

// output to browser / client
print $json;
exit();

Returns to client/browser:

{"x":"y","price":0.00}

Solution 3 - Php

0.00 is actually 0. If you need to have the 0.00 when you echo, simply use number_format this way:

number_format($number, 2);

Solution 4 - Php

You can show float numbers

  • with a certain number of decimals
  • with a certain format (localised)

i.e.

$myNonFormatedFloat = 5678.9

$myGermanNumber = number_format($myNonFormatedFloat, 2, ',', '.'); // -> 5.678,90

$myAngloSaxonianNumber = number_format($myNonFormatedFloat, 2, '.', ','); // -> 5,678.90 

Note that, the

1st argument is the float number you would like to format

2nd argument is the number of decimals

3rd argument is the character used to visually separate the decimals

4th argument is the character used to visually separate thousands

Solution 5 - Php

Use the number_format() function to change how a number is displayed. It will return a string, the type of the original variable is unaffected.

Solution 6 - Php

you can try this,it will work for you

number_format(0.00, 2)

Solution 7 - Php

try this

$nom="5695.5";
number_format((float)($nom), 2, '.', ','); // -> 5,695.50

$nom="5695.5215";
number_format((float)($nom), 2, '.', ','); // -> 5,695.52

$nom="5695.12";
number_format((float)($nom), 0, '.', ','); // -> 5,695

Solution 8 - Php

A number of comments on this page have missed the fundamental point that the question is ill-formed. Floating point is a binary representation, designed for efficient calculations; it fundamentally has no notion of decimal digits of any sort.

So asking this:

> How do I get "0.00" instead of "0" and still have the data type as a float?

Is like asking this:

> How do I get "deux" instead of "zwei" and still have the language as English?

Whether you specify the input as "2", or "2.0", or "2.000000000", if you ask for a floating point value, what will be stored in memory is this (in IEEE 754 double-precision):

> 0100000000000000000000000000000000000000000000000000000000000000

If you convert to an integer, the value stored in memory is this (assuming a 64-bit system):

> 0000000000000000000000000000000000000000000000000000000000000010

(Note that "integer" in this context is not just a synonym for "whole number", it is a specific data type, with its own rules for how values should be represented in memory.)

By contrast, the string "2" would look like this:

> 00110010

And the string "2.00" would look like this:

> 00110010001011100011000000110000

(In a PHP program, there would actually be additional information in memory, such as an indicator of the type, but that's not really relevant here.)


So, the question can only be answered by rephrasing it as a conversion: given the input of a floating point number, how do I choose a string representation which has a fixed number of decimals.

As others have pointed out, the answer to that is to use number_format.


The question doesn't mention JSON, but several comments do, so I will also point out that PHP's json_encode function has an option JSON_PRESERVE_ZERO_FRACTION, which will format a floating point number that happens to be a whole number with a trailing ".0", for instance:

$example = ['int' => 2, 'float' => 2.0];
echo json_encode($example);
# => {"int":2,"float":2}
echo json_encode($example, JSON_PRESERVE_ZERO_FRACTION);
# => {"int":2,"float":2.0}

Again, note that this is a string representation of the value.

Solution 9 - Php

You can use floatval()

floatval()

Solution 10 - Php

You can use round function

round("10.221",2);

Will return 10.22

Solution 11 - Php

try this

$result = number_format($FloatNumber, 2);

Solution 12 - Php

When we format any float value, that means we are changing its data type to string. So when we apply the formatting on any amount/float value then it will set with all possible notations like dot, comma, etc. For example

(float)0.00 => (string)'0.00',
(float)10000.56 => (string) '10,000.56'
(float)5000000.20=> (string) '5,000,000.20'

So, logically it's not possible to keep the float datatype after formatting.

Solution 13 - Php

You can use this simple function. number_format ()

    $num = 2214.56;
    
    // default english notation
    $english_format = number_format($num);
    // 2,215
    
    // French notation
    $format_francais = number_format($num, 2, ',', ' ');
    // 2 214,56
    
    $num1 = 2234.5688;
    
   // English notation with thousands separator
    $english_format_number = number_format($num1,2);
    // 2,234.57

    // english notation without thousands separator
    $english_format_number2 = number_format($num1, 2, '.', '');
    // 2234.57

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStackOverflowNewbieView Question on Stackoverflow
Solution 1 - PhpDenys SéguretView Answer on Stackoverflow
Solution 2 - PhpTim B.View Answer on Stackoverflow
Solution 3 - PhpAurelio De RosaView Answer on Stackoverflow
Solution 4 - PhpmareksView Answer on Stackoverflow
Solution 5 - PhpBart FriederichsView Answer on Stackoverflow
Solution 6 - PhpAmrish PrajapatiView Answer on Stackoverflow
Solution 7 - PhpWaruna ManjulaView Answer on Stackoverflow
Solution 8 - PhpIMSoPView Answer on Stackoverflow
Solution 9 - PhpDimuthu NilankaView Answer on Stackoverflow
Solution 10 - PhpjewelhuqView Answer on Stackoverflow
Solution 11 - PhpLove KumarView Answer on Stackoverflow
Solution 12 - PhpSenior PHP DeveloperView Answer on Stackoverflow
Solution 13 - PhpSani KamalView Answer on Stackoverflow