Josh Mayer
Josh Mayer

Reputation: 81

Can echo specify a floating-point number?

I can do this using printf()

printf ("Total amount of order is %.2f", $total);

Can I do the same using echo, or I have to use printf()?

Upvotes: 0

Views: 72

Answers (3)

Deepu Sasidharan
Deepu Sasidharan

Reputation: 5309

You can use this..

$total= 100.123;
echo "Total amount of order is ". round($total, 2);

Output:

Total amount of order is 100.12

Upvotes: -1

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

You cannot specify format using echo.

However if you need to assign result to string, you can simple use sprintf function:

$var = sprintf ("Total amount of order is %.2f", $total);
echo $var;

Upvotes: 0

James Hunt
James Hunt

Reputation: 2528

Using echo alone? No. You would include number_format in the echoed string for the same functionality.

echo "Total amount of order is ".number_format($total,2);

Upvotes: 4

Related Questions