0

I'm trying to store decimals taken from input in a Mysql decimal (10,2) row. But when I enter 37.50 in the input, Mysql only stores 37.00 It seems to disregard everything after the dot. How can I store decimals?

this the input

<input class='form-control formBlock' name='payrate' value='' step="0.01" type='number' placeholder="Pay Rate..." required>

So far I tried float:

$payrate=floatval($_POST['payrate']);
2
  • Cool story, bro. Was there a question? Commented Apr 2, 2017 at 4:26
  • @spencer7593: lol dude I'm laughing so hard right now. I forgot to ask. Sorry I will update. Commented Apr 2, 2017 at 16:19

1 Answer 1

1

It's unclear what you are asking. (Was there a question? Or was all that just intended as a status report?)

In a numeric context ...

MySQL interprets a "dot" character in a numeric literal as a decimal point.

MySQL interprets a "comma" character as invalid. MySQL reads the value from left to right until it hits an invalid character, and takes whatever it has read as the value.

As a demonstration, consider

SELECT '123,456.78' + 0     --> 123
SELECT '4t2' + 0            --> 4

If we need to pass a numeric value into MySQL that contains commas, and we want MySQL to disregard the commas, then we can remove the commas

SELECT REPLACE('123,456.78',',','') + 0  -->  123456.78

If we want a comma treated as the decimal separator, we can replace it with a dot character

SELECT REPLACE('456,78',',','.') + 0  -->  456.78

Of course, we could also do that string manipulation and cleanup of the value in the client, before we pass the value to MySQL.

Not sure if any of that answers the question you asked. Was there a question?

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

7 Comments

I have updated the question. What I meant to say is that the decimals after the dots do not get inserted in the database.
Ok great for this answer. One thing I dont understand why SELECT? I'm actually trying to do an insert?
@RickJames: As I understand it, yes, it's independent of locale. In MySQL, the dot character is the decimal separator in a string representation of a numeric value.
@SebastianFarham: We typically use a SELECT statement to demonstrate MySQL expressions. The expression would be evaluated in the same way in an INSERT. To answer the question as to why As why a value being inserted to a column 123.45 gets stored as 123... we'd really need to examine the code you are using. Is the value being cast to an integer value in the client, before it's sent to MySQL? Is there a BEFORE INSERT trigger that modifies the value? I recommend testing from the mysql command line client, to determine if inserting 37.50 works there.
@SebastianFarham: I would also add some debug code before the insert, var_dump the SQL text and any bind values, before the query execution, to see what is being sent to MySQL. That should help identify whether the issue is on the MySQL database, or in the client. If we are using a prepared statement with bind placeholders (which is what we should be doing) with mysqli, what is the datatype specified for the bind variable?
|

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.