juice.gin
juice.gin

Reputation: 71

How do I add a decimal before the last 2 characters using PHP

Okay now I have a problem that I need to add a decimal before the last two digits of a number for example.

Current Number

29200

Desired output

292.00

I currently dont know how to go about this.

Upvotes: 0

Views: 72

Answers (1)

Nick
Nick

Reputation: 147146

A few possibilities:

echo number_format(29200/100,2,'.','');
echo preg_replace('/(\d\d)$/', '.$1', 29200);
printf("%.2f", 29200/100);
echo bcdiv(29200, 100, 2);

Output (for all)

292.00

Links to the various manual pages: bcdiv, printf, number_format, preg_replace

Upvotes: 3

Related Questions