PHP Convert cents to dollars

14.7k views Asked by At

I'm using Stripe Payment. My prices are integers in cents:

050
1000
1250
2999

I manipulate those numbers with my own class and no problem with that.

I'm looking the way to convert those numbers (cents) in dollars (always with decimals even if .00) like following:

0.50
10.00
12.50
29.99

Any idea?

2

There are 2 answers

1
pavel On

You need to divide cents by 100 and use number_format function.

echo number_format($price/100, 2, '.', ' ');
0
Death-is-the-real-truth On

You can use number_format() in following way:

<?php

$value = 50;

echo number_format(($value /100), 2, '.', ' ');

?>

Output:- https://3v4l.org/tHigs

For more reference read the below link

number_format