Php parsefloat 2 decimal places

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.

How can I get 2 decimal places in PHP?

Use number_format() Function to Show a Number to Two Decimal Places in PHP..
Use round() Function to Show a Number to Two Decimal Places in PHP..
Use sprintf() Function to Show a Number to Two Decimal Places in PHP..
Related Article - PHP Number..

How do I put 2 decimal places in HTML?

Use the toFixed() method to format a number to 2 decimal places, e.g. num. toFixed(2) . The toFixed method takes a parameter, representing how many digits should appear after the decimal and returns the result.

How does PHP calculate float value?

Dealing with floating-point calculations in PHP.
1 – work on Integers. echo intval((0.1*10) + (0.7*10)); // int(8) ... .
2 – the round() function. echo intval(round((0.1+0.7)*10, 0)); // int(8) ... .
3 – casting to string. echo intval(strval((0.1+0.7)*10)); // int(8) ... .
4 – BC Math. ... .
Summary..

How do you float in PHP?

Use the Type Casting to Convert a String to Float in PHP..
Use the floatval() Function to Convert a String to Float in PHP..
Use the number_format() Function to Convert a String to Float in PHP..