My (MySQL) tables have columns with currency values set as Decimal(10,2) with decimals separated by commas. However whenever i query them, php turns all my commas into periods and invalidates my results. Can I stop it doing that? It affects both my SELECT and my INSERT/UPDATE.
When I select, for example, 123,00 php turns it into 12300.00; same for inserting a number: i insert 123,00 but the value in the table becomes 12300,00 again. Is there a way to make comma the default decimal separator?
have you looked in to
http://php.net/manual/en/function.money-format.php
$number = 1234.56;
// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
For the PHP part this might me helpful
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
This should give you a clue on how to get the right output from your PHP code.
Don't care on how the database stores your numbers, neither on how PHP does it internally.
Use the number_format to get your desired output.
See also PHP: number-format for more on that.
Related
I am using cms - megento. I want to display the price value in following format :
add comma after every 3 digits.
for example :
$price = 987536453 ;
Need to print like 987,536,453.
Try using the number_format function.
By default it prints ',' every 3 digits and cuts decimal:
echo(number_format(1234));
1,234
Edit:
as another answer suggested, there is a simple way to do this using number_format:
echo number_format(1234); // 1,234
Original answer:
try this str_split
$price = 1234;
$price_text = (string)$price; // convert into a string
$price_text = strrev($price_text); // reverse string
$arr = str_split($price_text, "3"); // break string in 3 character sets
$price_new_text = implode(",", $arr); // implode array with comma
$price_new_text = strrev($price_new_text); // reverse string back
echo $price_new_text; // will output 1,234
You can use number_format to group by thousands
You can use either number_format or money_format to do this.
number_format - http://in2.php.net/number_format
money_format - http://www.php.net/manual/en/function.money-format.php
how to convert all numbers to xx,xx decimal format without exploding number or string?
like;
8,9 --> 8,90
8,99 --> 8,99
12,1 --> 12,10
129,9 --> 129,90
any idea?
You can use number_format like:
$n = 2.1;
echo number_format($n, 2, ','); // 2,10
If you have commas as decimal separators in your input you can convert values to float with:
$number = floatval(str_replace(',', '.', str_replace('.', '', $string_number)));
str_replace('.', '', $string_number) is used to remove thousand separators.
str_replace(',', '.', ... ) is used to replace commas with dots.
If you have the intl extension enabled, the best approach to the issue (IMO) is given in the accepted answer to this question: PHP: unformat money
You can use
NumberFormatter::parseCurrency - Parse a currency number
Example from Manual:
$formatter = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
var_dump($formatter->parseCurrency("75,25 €", $curr));
gives: float(75.25)
Note that the intl extension is not enabled by default. Please
refer to the Installation Instructions.
After that, you'll have a float value and formatting that is a trivial issue using number_format().
If you do not have that extension, check out the regex approach in this question: What a quick way to clean up a monetary string
$money = number_format($number, 2, ",", ","); // $number must be a float, i.e 8.8
If your inputs have to have commas as decimal separators, do this too:
$money = "20,2";
$fixed_money = floatval(str_replace(",", ".", $money));
echo number_format($fixed_money, 2, ",", ",");
str_pad will add the missing zero
$val = '1008,95';
$len = strpos($val, ',') + 3;
$newval = str_pad($val, $len, '0', STR_PAD_RIGHT);
echo "Value: " . $newval . "<br>";
You have to be sure that your values don't have more than two decimal digits, though
I made a composer package that can deal with such things depending on which currency you are displaying.
https://packagist.org/packages/votemike/money
If you also enter the country it will format the currency in the local way (with or without commas and with the correct number of decimal places)
$money = new Money(99.50, 'JPY');
$money->format(); //¥100
And so if you just create a new money object with the amount you want to display and the currency you are displaying it in, calling ->format() on that object should give you the result you're looking for.
I have a CakePHP 1.3 website with a MySQL database. The problem is that i have some prices, double values, that are stored using a dot as a decimal delimiter, however my application front-end should display them using commas.
Is there a way to set this in the DB directly, or simply a better way to achieve this than str_replace EVERYWHERE? that would be such a hassle, and it just seems like bad practice to me.
Thank you in advance!
The decimal in the database is correct- it's a decimal being used as a decimal. To display it, you should use number_format for presentation.
You could technically just do n str_replace in the model's afterFind handler and forget about it.
$decimalNumberFromDb= 1234.56;
//
// French notation
//
$frenchNotation = number_format($decimalNumberFromDb, 2, ',', ' '); // Output: 1 234,56
//
// English Notation
//
$englishNotation = number_format($decimalNumberFromDb, 2, '.', ''); // Output: 1234.56
The dot in your database is not a delimiter but the way decimals are stored.
You could do a select query like this
select *, REPLACE(CAST(price AS CHAR), '.', ',') as price_formatted from products;
Or with display it with PHP in other formats with number_format
$price = 1300222.99;
echo "Original: ".$price.PHP_EOL;
echo "english/us: ".number_format($price,2).PHP_EOL;
echo "Dutch/german: ".number_format($price, 2, ',', '.').PHP_EOL;
echo "French: ".number_format($price, 2, ',', ' ').PHP_EOL;
echo "Swiss: ".number_format($price, 2, ',', '\'').PHP_EOL;
example at http://codepad.org/WXjc7d2D
you can use behaviors which do that for you:
https://github.com/dereuromark/tools/blob/2.0/Model/Behavior/DecimalInputBehavior.php
I have script that identifies with preg_match_all some numbers from a given file and in a given format '#(\d\,\d\d\d\d)#' (decimal, with 4 decimals). With them, later, I need to do some math operations to find out the sum, average etc.
With print_r I can see all matches from the array and it is ok (4,3456, 4,9098, etc.). I verify the type of variables and gettype() returned string
Unfortunately I cannot do math operations with them because when I use the variables in a math expression the result is always rounded regardless of what came afer the comma.
For example:
4,3456 + 4,9098 + 4,3456 = 12, or 12,0000 -- if I use number_format.
I used . instead of , in the numbers, I formatted the results with number_format, but have had no success. It seems I am missing something.
Thanks for help!
The error happens even before the number_format call -- PHP considers . as the decimal separator, not ,. you need to str_replace all your array elements:
$values_array = str_replace(",", ".", $values_array)
PHP uses the . character as decimal separator, so you have to replace the , by a . in your matched numbers before converting them to numbers:
$number = floatval(strtr("1,234", ",", "."));
// 1.234
Example:
<?php
$numbers = array("1,234", "5,67");
$numbers = str_replace(",", ".", $numbers);
echo number_format($numbers[0] + $numbers[1], 4, ',', ' ');
Try it here: http://codepad.org/LeeTiKPF
I'm doing a little application of adding prices and decimals. Points are normal to use with decimals, but how can I write decimal number with comma as input (543,35 instead of 543.35) and then maybe change it with point to the database (mysql)? Then print it back form the database with the comma. Reason is that comma (,) is more used in Finland than point (.) when write decimal numbers.
Thank you very much!
Samuel
$input = '5,50';
$output = str_replace(',', '.', $input);
var_dump($output); // string(4) "5.50"
$number = (float)$output;
var_dump($number); // float(5.5)
you need not do anything in the sql end. you want to format the decimal value in php (this assumes php4/php5): Set the third parameter $dec_point to ','
// string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )
<?php
$number = 1234.56;
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
source:
http://php.net/manual/en/function.number-format.php
Cheers!
the PHP number_format function is what you need :
number_format(5.50, 2, ',');
...should do the trick.
As it's been said, it's best to save the value as a float and then format for display. As an alternative to the previously suggested number_format(), you could try money_format() http://us.php.net/manual/en/function.money-format.php It doesn't work in a windows environment though if that is important to you.
No, using comma as a decimal separator for arithmetic operations is not supported. Filter your input and replace the comma with a period, then format your output as you wish.