Php define variable float has two decimal places

MySQL imoprt mongo database.

price float(15,2) in mysql, mongo is not float(15,2).

      

I want to define that var $price

has two decimal places.

eg. 100.00 is correct, 100 or 100.0 is incorrect.

eg.1

$price = 100.00;

      

$price

have two decimal numbers, that's correct.

eg.2

$price = 100.0;

      

$price

do not have two decimal places, this is wrong.

+3


source to share


6 answers


Everyone is dancing around the fact that floating point numbers do not have decimal places internally. that is, in float 100 == 100.0 == 100.00 == 100.000 and they are all represented by the same number, effectively 100 and stored that way.



The number of decimal places in this example only has context when the number is represented as a string. In this case, any string function that counts the number of digits ending with a decimal point can be used for verification.

+2


source


I like to use regular expressions to do these things

function validateTwoDecimals($number)
{
   if(preg_match('/^[0-9]+\.[0-9]{2}$/', $number))
     return true;
   else
     return false;
}

      



(Thanks to Fred-ii- for the fixes)

+4


source


For this to work, the number must be enclosed in quotation marks.

With many of the scenarios I've tested, using $price = 100.00;

without quotes didn't work, but $price = 100.10;

- in a way that's better than it works.

<?php 
$number = '100.00';
echo $number.'<br>';
$count = explode('.',$number);

echo 'The number of digits after the decimal point is: ' . strlen($count[1]);

if(strlen($count[1]) == 2){
echo "<br>";
echo "There is 2 decimal points.";
}

else{
echo "<br>";
echo "There is not 2 decimal points.";
}

      

+2


source


After formatting the value, you can check by simply splitting the value as a string in two, for example with explode ...

$ex=explode('.',$in,2); if (strlen($ex[1])==2) 
{
 // true
}
else
{
 // false
}

      

But then again, since I already commented, if you actually have floating input, this is just not a reliable way, as floating numbers have no decimal places, even if they appear due to rounding to float => string conversion

What can you do if you really have floating numbers and want to have numbers in the xxx.yy format:

1) convert float to string using round ($ x, 2), so it will be rounded to two decimal places. 2) blow up the number as I described and do this:

while (strlen($ex[1]<2)) {$ex[1].='0';}
$number=implode('.',$ex);

      

0


source


I would use the following function for this:

function isFloatWith2Decimals($number) {
    return (bool) preg_match('/^(?:[1-9]{1}\d*|0)\.\d{2}$/', $number);
}

      

This also checks if you only have one leading 0, so a number like this 010.23

will not be considered valid, whereas a number like 0.23

it will.

And if you don't care about 0 lead, you can use a simpler method:

function isFloatWith2Decimals($number) {
    return (bool) preg_match('/^\d+\.\d{2}$/', $number);
}

      

Of course the digits must be passed as a string - if you pass 100.00

it will not be considered true, whereas '100.00'

would

0


source


number_format($price, $numberOfDecimalDigits) === $price;

      

or

strrpos($price, '.') === strlen($price) - 1 - $numberOfDecimalDigits;

      

General information: $price

should not be called a "floating variable". It is a string that is a float value. 100.00

since float has zero decimal digits and 100.00 === 100

float:

$price = 100.00;
echo $price; // output: 100
$price2 = (float)100;
echo $price === $price2; // ouput: 1

      

0


source







All Articles