Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add numbers after decimal using php

I am having trouble thinking of a php function to add zeros after a decimal. Let say I have $money="10000" I need a function that will add .00 to 10000.00 and to add just a zero to 0 after 234.5. can anyone help me please?

like image 541
Aadi Avatar asked Nov 11 '10 07:11

Aadi


People also ask

How can I add two digit numbers after decimal in PHP?

Example #1 round() examples php echo round(3.4); // 3 echo round(3.5); // 4 echo round(3.6); // 4 echo round(3.6, 0); // 4 echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 echo round(5.045, 2); // 5.05 echo round(5.055, 2); // 5.06 ?>

How do I round to 2 decimal places in PHP?

Parameter Values Specifies a constant to specify the rounding mode: PHP_ROUND_HALF_UP - Default. Rounds number up to precision decimal, when it is half way there. Rounds 1.5 to 2 and -1.5 to -2.

How do you round off decimals in PHP?

The round() function in PHP is used to round a floating-point number. It can be used to define a specific precision value which rounds number according to that precision value. Precision can be also negative or zero.

What is number format PHP?

The number_format() function is an inbuilt function in PHP which is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure. Syntax: string number_format ( $number, $decimals, $decimalpoint, $sep )


1 Answers

Just as Dan Grossman said; number_format is your friend here and you'd accomplish those double zeros by:

$money = "1234";
$formatedNumber = number_format($money, 2, '.', '');
echo $formatedNumber;

// 1234.00
like image 108
Xenovoyance Avatar answered Sep 28 '22 08:09

Xenovoyance