Matt Elhotiby
Matt Elhotiby

Reputation: 44086

how do i round in php with keeping the two zeros

I have this php

<?php echo round($price, 2); ?>

and the $price maybe 1.0000

i want 1.00 but i get only 1

any ideas

Upvotes: 32

Views: 57459

Answers (3)

John Godspeed
John Godspeed

Reputation: 1407

number_format works:

echo number_format($price, 2);

Upvotes: 79

user142162
user142162

Reputation:

The following printf() call should work for you:

<?php printf("%.2f", $price); ?>

The documentation for this syntax is best described on the sprintf() page.

Upvotes: 38

lsl
lsl

Reputation: 4419

number_format is your best bet.

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

Example:

<?php echo number_format(1.0000, 2, '.', ','); ?>

Upvotes: 17

Related Questions