469,282 Members | 1,953 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

number_format()

TheServant
1,168 Expert 1GB
I have realized something which is going to be a headache in the rest of my current project. I don't know how I have not come across this before but let me show you an example:
Expand|Select|Wrap|Line Numbers
  1. $x = number_format( 10000 );
  2. $y = 3;
  3. echo( $x*$y );
You would expect 30,000 right? Because the first number format makes that number 10,000 it is now a string (I think) and when operated upon, it converts it to a number as if it were 10.000 (ie just 10).

How can I get around this? Am I going to have to double up on all my variables and only number_format them after they are 100% done with?
Mar 11 '09 #1
17 3403
Dormilich
8,651 Expert Mod 8TB
the string "10,000" is cut before the comma while conversion back to number.
Mar 11 '09 #2
TheServant
1,168 Expert 1GB
Yeah, well in Australia we're the opposite. How can I change that?
Mar 11 '09 #3
Dormilich
8,651 Expert Mod 8TB
sorry, but the locale isn't used in number_format().... (but in some other *_format() functions). corrected that (read again).
Mar 11 '09 #4
Markus
6,050 Expert 4TB
number_format() returns a string. So, best way to go about this, is to have your integer in it's own variable $x and then have it's formatted version in another variable $x_formatted.
Mar 11 '09 #5
TheServant
1,168 Expert 1GB
OK, so is there a way to change it so that it recognises 10,000 as 10000? Changing something in the php.ini file? I have been trying to search about changing the locale but nothing yet about php.ini?
Mar 11 '09 #6
TheServant
1,168 Expert 1GB
@Markus
Bugger, I really didn't want to do it that way. Thanks for your help.
Mar 11 '09 #7
Dormilich
8,651 Expert Mod 8TB
@TheServant
remove any non-digits and non-commas from the string and the string-number conversion should run smoothly.

or use Markus' way
Mar 11 '09 #8
TheServant
1,168 Expert 1GB
Unfortunately what I will have to do is format them as they are echoed, which is just cramping my php/html separation :(
Mar 11 '09 #9
Dormilich
8,651 Expert Mod 8TB
why that (don't understand)?
Mar 11 '09 #10
TheServant
1,168 Expert 1GB
Well instead of making another (formatted) variable for each variable I need to format (most of them), it's easier to just format them as they come out.
Expand|Select|Wrap|Line Numbers
  1. $x = 10000;
  2. $y = 3;
  3. $z = $x * $y;
  4. echo( number_format($x) . " " . number_format($z) );
Compared to:

Expand|Select|Wrap|Line Numbers
  1. $x = 10000;
  2. $x_formatted = number_format($x);
  3. $y = 3;
  4. $z = $x * $y;
  5. $z_formatted = number_format($z);
  6. echo( $x . " " . $z );
Mar 11 '09 #11
Dormilich
8,651 Expert Mod 8TB
some speed-up:
Expand|Select|Wrap|Line Numbers
  1. echo number_format($x), " ", number_format($z);
Mar 11 '09 #12
TheServant
1,168 Expert 1GB
Really? I have seen that before but I have always joined strings with "."? That's definitely faster?
Mar 11 '09 #13
Dormilich
8,651 Expert Mod 8TB
the point there is: with the commas, you dont need to do the string concatenation thus saving time an output (the Zero bytes). see PHP Performance
Mar 11 '09 #14
TheServant
1,168 Expert 1GB
Makes sense. Thanks for the tip (and the link)... Really gtg bed. Thanks for the chat guys.
Mar 11 '09 #15
Markus
6,050 Expert 4TB
@Dormilich
because PHP allows for function style arguments in echo() - only echo, though. Not print().

Good night.

- Mark.
Mar 11 '09 #16
devsusen
136 100+
@Dormilich
Oh lovely options. I must put this into my codeing habits. Thank for the information. :)
Mar 12 '09 #17
The number_format() function formats a number with grouped thousands.
Example
<?php
echo number_format("1000000");
echo "<br />";
echo number_format("1000000",2);
echo "<br />";
echo number_format("1000000",2,",",".");
?>

you can find more easy solution here
<url removed: no advertising please>
thanks
Mar 12 '09 #18

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by tencip | last post: by
5 posts views Thread by Edward Hartfield | last post: by
7 posts views Thread by kkmigas | last post: by
9 posts views Thread by rynTAU | last post: by
1 post views Thread by whitey | last post: by
reply views Thread by geraldjr30 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.