Converting input number to Mysql decimal

﹥>﹥吖頭↗ 提交于 2020-01-07 05:24:09

问题


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']);

回答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?



来源:https://stackoverflow.com/questions/43164412/converting-input-number-to-mysql-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!