5

i ran into a really strange problem with storing values in MySQL. The premise:

I have a table that uses DECIMAL(15,8) to store monetary values (like the total of order), but when i try to insert for example:

2,45545345

this is stored as

2.00000000

I tried MySQL's FORMAT/CAST functions but still the same output.

This is how the query gets generated:

$db->query("INSERT INTO `random_table_name` SET currency_value = '" . floatval($value) . "'");

i also tried doubleval, but same result. The funny thing is though that this same piece of code was working fine a couple of weeks ago and i can't recall any changes to the db structure or the db class that can cause this.

4
  • 1
    Does your script use setlocale? Commented Jan 21, 2013 at 8:29
  • Yes, but i can't turn it off or remove it. Commented Jan 21, 2013 at 8:34
  • What locale is it? floatval will convert "2,45545345" to 2.0 or 2.45545345 depending on locale. Commented Jan 21, 2013 at 8:38
  • It should work in multi-language environment, so that's why i'm looking for a bit more generic approach. The current issue though is with ro_RO Edit:: Setting the locale to en_US does the trick, but is there another (cleaner) way? Commented Jan 21, 2013 at 8:42

2 Answers 2

8

Use number_format to replace the , with .

Like this:

number_format($value, 8, '.') // 8 = number of decimals, . = decimal separator

However, your problem seems to be related to the current locale. You need to look into the following: setlocale() and localeconv

setlocale(LC_ALL, 'en_US'); // NOT TESTED, read up on the appropriate syntax

This is the appropriate way of doing this, the alternative would be (as suggested below), to do a str_replace(',', '.'), but you have to do the reverse every time you want to output strings.

There is another option though, you can set the MySQL locale to en_US.

Sign up to request clarification or add additional context in comments.

3 Comments

Not a solution as it produces the same result - 2.0000000000
Indeed. You need to set the appropriate locale first. US locale should do the trick, not sure about the syntax, so I added the links so you can look it up.
Thank you Vlad, setlocale as suggest by Salman is the culprit here!
3

Replace the , with . before inserting into db:

$value = str_replace( ',', '.', $value);

This will create a valid number, that can be safely inserted into the database. Or just add it just inside your query:

INSERT INTO `random_table_name` SET currency_value = '" . str_replace( ',', '.', $value ) . "'

3 Comments

One of the first things i tried - doesn't work, the value gets rounded to 2 by the DB
str_replace can't round your number to 2
str_replace does not, but the DB stores the value as 2.000000 even when using MySQL's CAST/FORMAT functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.