showing the decimal places

This is a discussion on showing the decimal places within the PHP General forums, part of the PHP Programming Forums category; when I define a number as $number = 2.00 then echo it the number shows as 2. How can I ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-17-2005
ross@aztechost.com
 
Posts: n/a
Default showing the decimal places

when I define a number as

$number = 2.00

then echo it the number shows as 2. How can I get the two zeros to show?

This is not in any of my books but a fairly easy solution I'll bet!

R.
Reply With Quote
  #2 (permalink)  
Old 03-17-2005
Rob Agar
 
Posts: n/a
Default RE: [PHP] showing the decimal places

> From: ross@aztechost.com [mailto:ross@aztechost.com]
> when I define a number as
>
> $number = 2.00
>
> then echo it the number shows as 2. How can I get the two
> zeros to show?
>
> This is not in any of my books but a fairly easy solution I'll bet!


the function you want is called sprintf (s for string, f for format)
summat like this:

echo sprintf('%0.2f', $number);

hth,
Rob Agar
Reply With Quote
  #3 (permalink)  
Old 03-17-2005
Jochem Maas
 
Posts: n/a
Default Re: [PHP] showing the decimal places

ross@aztechost.com wrote:
> when I define a number as
>
> $number = 2.00
>
> then echo it the number shows as 2. How can I get the two zeros to show?
>
> This is not in any of my books but a fairly easy solution I'll bet!


please consider php.net as your book too ;-)

have fun with the following:

<?php

$newLine = ($viaWeb = isset($_SERVER["HTTP_HOST"]))
? "<br />"
: "\n";

$number = 2.00;

echo $number; echo $newLine;
var_dump( $number ); if ($viaWeb) { echo $newLine; }
printf("%0.2f", $number); echo $newLine;
echo number_format( $number, 2 ); echo $newLine;
echo sprintf("%01.2f", $number); echo $newLine;
echo number_format(123456789, 2, ",", " "); echo $newLine;

>
> R.
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:13 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0