I've seen a few questions on people trying to get MySQL to use ',' as the floating point separator - what I'm trying to do is stop PHP from using it on a website running under the 'nl_NL' locale.
So in the code PHP is writing an SQL query ending like:
" ... HAVING `relevance` >= {$fFloatingPointNumber}";
The problem is, because PHP's locale is running as 'nl_NL' when it converts that floating point number to a string it's using ',' as the separator (e.g. 1,5).
What I'm doing to prevent this currently is:
" ... HAVING `relevance` >= " . number_format($fFloatingPointNumber, 2, '.', '');
Is there a better way of doing this - or is this my best bet?
Solution 1
I will post my answer in case it is the only solution apart from #Pete one.
I would suggest switching the locale to GB or some other period separated float/double locale before each query then switching back to the correct locale after. I can not think of any other way around this.
Solution 2 (Best Bet)
You could always use the number_format method as follows
$stringversion = number_format($theFloat, 2, ".","");
Pretty sure this would work, the documentation is here http://php.net/manual/en/function.number-format.php
Could you try this or something similar? Credit to Ludovico Grossi
<?php
setlocale(LC_ALL, 'nl_NL');
setlocale(LC_NUMERIC, 'en_GB'); //overwrite the decimal separator
?>
Related
when i get data from database and export it as a csv file i have following issues :
the leading zero of the numbers dosnt show
big numbers shows like this -> 1E+12
how to force MS Excel to show all of my data as string by PHP
You can use double quotes contain the numbers, like this:
if(is_numeric($column)) $column = '"'.$number.'"';
This isn't a MySQL issue, it's an Excel thing.
This... might help: http://answers.microsoft.com/en-us/office/forum/office_2007-excel/disabling-scientific-notation/943b8103-8c50-451d-8037-c697422e2307
But this question is more MS Office related than programming.
(or as comments suggested, use a file format that carries display formatting info, such as .xls)
I format it as a string by concatenate it with spaces at its start and end.
CONCAT(" ", database_number, " ") AS "Number For Excel".
Kai's answer didn't quite work for me. I ended up CONCATing an equals sign (still with the double quotes on either end) to the front and got the desired result. Our users wanted to be able to copy and paste the number straight out of Excel into our UI.
CONCAT('="',too_long_number_field,'"')
You can use number_formate() function to convert ...
View link
Function : number_format()
Syntax : number_format ( float $number , int $decimals = 0 , string $dec_point = ‘.’ , string $thousands_sep = ‘,’ )
Example to convert 6.90743E+11 to number use below code
number_format(6.90743E+11,0,'','') // outputs 690743000000
I have a Postgre DB and one of these values are money (type, ofc).
When I do the Select using PHP, the output of that query is in this format:
Example: €1,00
I'm looking for something to change this format to this other:
Example: 1,00€.
What can I change in my query to get last one result?
Thanks!
Fastest way in PHP is to use str_replace :)
$cash = '€1,00';
$cash = str_replace('€','',$cash) . '€';
echo $cash;
But a better option is to change your DB field type to some numeric type and just to add the ' €' sign where you want it..
You may do this replace inside your DB SELECT statement but I think that its better to do that inside your PHP..
If its stored in database just like a number without currency you could just use function number_format($x,y,'.','')
where
$x is your number
y is number of numbers behind decimal separator
'.' - means decimal separator is .
'' -means there is no separator between thousands
so u just format your number in a way u want and add "euro" sign.
if its price there is a function called money format :
u must use it with locales of country the currency u want fe. :
setlocale(LC_MONETARY, 'de_DE');
echo money_format('%.2n', $your_variable);
this means u use currency of germany. '%.2n' means your output is from $yourvariable and have 2 decimalnumbers
And to the thing of 'euro' sign after or before number. It depends of locales. I think slovak or spanish has euro sign after number.
Try it.
This should provide you what you need, and be flexible across all currencies:
$value = '$4265';
if (!is_numeric($value[0])) {
$value = substr($value, 1) . $value[0];
}
I'll try and keep this simple. I'm using a BIGINT data type on a MySQL database table. I have a function generating a unique random number that may span anywhere from 12 digits to 13 digest long.
When I insert into the database with a digit that is 12 digits in length, it enters it just fine,
but when I use a value that is 13 digits or longer, it seems like it rounds up or something. here is the
php
$defaultText = 'some string'; //just some string
$web_id = 12;
$element_id = 23112182735363; //case 1 (doesn't work)
//$element_id = 2311218333205; //case 2, does work ()
mysql_query("INSERT INTO tableName (web_id, element_id, content)
VALUES ($web_id, $element_id, '".mysql_real_escape_string($defaultText)."')");
results:
in case one, it inserts a slightly different number, usually rounds up for some reason.
in case two, it inserts the number just fine! Maybe someone can help shed some light on this mystery! Thanks again!
the big int datatype:
bigint(200)
Numbers lose their precision from PHP_INT_MAX onwards. See also: http://www.php.net/manual/en/reserved.constants.php#constant.php-int-max
After that they are turned into floats which have limited precision and causes weird rounding issues. The only way to support BIGINT in PHP is by using strings.
I assumed you were talking about a 32-bit server.
But in my server, PHP seemed not lose the precision.
echo(PHP_INT_MAX . "<br/>");
$big_int = -6174803190685607000;
echo($big_int . '<br/>');
output
9223372036854775807<br/>-6174803190685607000<br/>
Sadly I still got the precision losing. I guessed it might because i used 'i' in prepare statement of mysqli, but I could not prove it.
I was trying to export database query to CSV and the need arised to use different decimal comma. It seems impossible to change the decimal comma in MySQL, so I tried in PHP:
setlocale(LC_NUMERIC, "cs_CZ");
But it seems that all the database functions like mysql_fetch_row and mysql_fetch_assoc are retrieving data of type string instead of double:
$res = mysql_query("select 50/3");
$row = mysql_fetch_row($res);
var_dump($row); // $row[0] is of type "string"
So in general PHP already doesn't have data of type double, but only strings!
So is there some general, clean way to specify output of the decimal point?
I ended up converting the strings using str_replace('.', ',', $row[0]) but this is ugly for two reasons:
you have to know which field is of type double
it's a dirty string job.
I don't know which database client/driver you're using but there is something like mysql_field_typeDocs which gives you the type based on it's offset, like 0.
This should do the job to find out if a certain column needs re-formatting or not.
To reformat, there is number_formatDocs.
With these you can do the conversion automatically.
Edit: Regarding your comments:
If you want to get PHP datatypes mapped, consider using the MySQL Native DriverDocs. Use it together with PDO:
Advantages of using mysqlnd for PDO
mysqlnd returns native data types when using Server-side Prepared Statements, for example an INT column is returned as an integer variable not as a string. That means fewer data conversions internally. Source
So depending of what you try to achieve, use the right tool.
See as well the multiple options you have when fetching data from a PDO StatementDocs.
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
Try
number_format($row[0], 2, ",", ".");
That should change the format of the number. (german format)
I have a cell with a great number, like 650400300. I need to SELECT this number and separate to receive next format - 650,400.300. Would you mind to help me, please. Thanks in advance! I'm using PHP and MySQL.
UPDATE: Now I think the correct way to do so is to use PHP, but thank you "hd1" your answer is perfectly fits and works correctly with what I asked here.
UPDATE2: I don't need to add zero's to the end, but I need to split the whole number into peaces with commas and dots.
Use the MySQL format function to do this:
MySQL> SELECT FORMAT(12332.2,0);
12,332
Let's assume you want php and not turning mysql into a gui tool, then
number_format($number)
Doing this with MySQL is not really nice, it is supposed to give you what is in the database. If the database holds an integer, MySQL should give you an integer.
You can do it with php, after you have fetched the data using number_format:
$formatted_number = number_format($number);
Note:
First, you must fetch that data:
Then use substr_replace to format the number
Code:
$number = "650400300";
$number = substr_replace($number, ",", 3, 0);
$number = substr_replace($number, ".", 7,0); /* RETURNS 650,400.300 */
Using number_format() to your given example (650400300) will give this result:
$number = number_format($number); /* 650,400,300 */