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: - $x = number_format( 10000 );
-
$y = 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?
17 3492
the string "10,000" is cut before the comma while conversion back to number.
Yeah, well in Australia we're the opposite. How can I change that?
sorry, but the locale isn't used in number_format().... (but in some other *_format() functions). corrected that (read again).
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.
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?
@Markus
Bugger, I really didn't want to do it that way. Thanks for your help.
@TheServant
remove any non-digits and non-commas from the string and the string-number conversion should run smoothly.
or use Markus' way
Unfortunately what I will have to do is format them as they are echoed, which is just cramping my php/html separation :(
why that (don't understand)?
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. - $x = 10000;
-
$y = 3;
-
$z = $x * $y;
-
echo( number_format($x) . " " . number_format($z) );
Compared to: - $x = 10000;
-
$x_formatted = number_format($x);
-
$y = 3;
-
$z = $x * $y;
-
$z_formatted = number_format($z);
-
echo( $x . " " . $z );
some speed-up: - echo number_format($x), " ", number_format($z);
Really? I have seen that before but I have always joined strings with "."? That's definitely faster?
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
Makes sense. Thanks for the tip (and the link)... Really gtg bed. Thanks for the chat guys.
@Dormilich
because PHP allows for function style arguments in echo() - only echo, though. Not print().
Good night.
- Mark.
@Dormilich
Oh lovely options. I must put this into my codeing habits. Thank for the information. :)
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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?...
|
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...
|
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
|
by: geraldjr30 |
last post by:
hi,
i have the following:
<html>
<STYLE type="text/css">
TD{font-family:ARIAL;font-size:11px;color:#666666;}
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
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...
|
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...
|
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...
|
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...
| |