472,333 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

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 3492
Dormilich
8,658 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,658 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,658 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,658 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,658 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,658 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

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

Similar topics

2
by: tencip | last post by:
Hi everyone, So, I'm running data through an array, and having a little difficulty in using the number_format function. You see, I'm trying to...
5
by: Edward Hartfield | last post by:
I'm am relatively new to Python but use it daily. Today, I went looking for a function, like PHP's number_function, that will take a number and...
3
by: Chifo | last post by:
hello. i have a problem with a populate html table with data from table here it's the problem two querys retrieving data from table, one of...
7
by: kkmigas | last post by:
Can some one explain if this can be fixed using php.ini settings ? echo "round 20.545 -".round(20.545,2)."<br>"; echo "round 20.555...
9
by: rynTAU | last post by:
This is an easy question, I'm sure but I can't seem to figure it out. I'm trying to use the number_format() function to remove all decimal...
19
by: thejobdone | last post by:
I need my webpage to clear tempoary internet files when a user visits my web page so that the images in my webpage www.fcestates.co.uk/index.php?...
4
by: jej1216 | last post by:
I have a PHP page that displays in a table budget vs. actual figures from a MySQL db. I want to add logic where if the actual is greater than the...
1
by: whitey | last post by:
Hi All, How do i define the number_format function to display the currency. it is showing dollars, and i want pounds thanks
0
by: geraldjr30 | last post by:
hi, i have the following: <html> <STYLE type="text/css"> TD{font-family:ARIAL;font-size:11px;color:#666666;}
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.