user3367639
user3367639

Reputation: 597

Format numbers correctly in php

I am trying to format 2176 as 21.76 in php

I have this code: $Payment->amount equals 2176 in this example.

$<?php echo number_format($Payment->amount,'2')/100; ?>

I get 0.02 Why?

Upvotes: 2

Views: 50

Answers (2)

John Conde
John Conde

Reputation: 219794

You need to do the division before you format the number:

$<?php echo number_format($Payment->amount/100,'2'); ?>

Upvotes: 2

xdazz
xdazz

Reputation: 160833

number_format($Payment->amount,'2') gives you string '2', then you divide it by 100, so the result is 0.02.

It should be:

$<?php echo number_format($Payment->amount / 100, 2); ?>

Upvotes: 4

Related Questions