sark9012
sark9012

Reputation: 5747

How to format pence as pounds and pence?

I am attempting to read out a value from the database in pence.

For example, if I read out 1345 pence, this is £13.45.

What is the best way to ALWAYS put a dot after the first 2 decimal places?

Upvotes: 3

Views: 4356

Answers (3)

MattJHoughton
MattJHoughton

Reputation: 1118

You need to watch out for numbers ending in 0. Dividing by 100 will remove them in your final price.

EG: 1290/100 will return 12.9.


Try using number_format

number_format($price/100, 2);

$price = 1345;
//returns 13.45

$price = 1200
//returns 12.00

$price = 1290
//returns 12.90

Upvotes: 7

rajah9
rajah9

Reputation: 12339

Look at this MySQL math function reference.

How about

Select format(amt/100.0,'#,###,###.##') from mytable

Upvotes: 0

LukeH
LukeH

Reputation: 269438

Divide it by 100?

1345 / 100 = 13.45

Upvotes: 4

Related Questions