-1

javascript Number function is handy when dropping undesired zero if decimal part is zero e.g

Number(2.00)

2

Is there such function in php or any alternative

sakhunzai
  • 13,900
  • 23
  • 98
  • 159
  • no , removing decimals if they don't make any sense , e.g `Number(1.20) => 1.20` but `Number(1.00) => 1` – sakhunzai Feb 09 '17 at 06:48

4 Answers4

4

I think this works that way by default in PHP. If you use proper number type like float or double.

If you're using string then you need to map

$a = '2.00';
echo (float)$a; // 2

Example of using float

$a = 2.00;
echo $a; //2

or

2.00 + 0; //2

If you want to format the number to show decimal part 2.00 you need to use number_format function (http://php.net/manual/en/function.number-format.php)

$a = 2.00
echo number_format($a, 2); // 2.00
Piotr Pasich
  • 2,639
  • 2
  • 12
  • 14
0

you can use intval() or any roundoff function

$_float = 1.9020441;
$_float = explode(".", $_float);
echo strlen($_float[1]);`
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Salman
  • 67
  • 4
0

You can achieve this by adding zero ... for example

echo "Result " . (2.00 + 0) . "\n";

Result 2
FreudianSlip
  • 2,870
  • 25
  • 24
0

you can use floatval()

echo floatval('2.00'); //2
echo floatval('2.23'); //2.23

See the documentation for more info

Tom
  • 4,257
  • 6
  • 33
  • 49
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30