17406

Converting input number to Mysql decimal

Question:

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?

<strong>this the input</strong>

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

Answer1:

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

In a numeric context ...

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

MySQL interprets a "comma" character as <em>invalid</em>. 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 <hr />

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.

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

Recommend

  • About C++ Payroll
  • Angular 6 Reactive Forms Custom Validator getting error shown from Default Data
  • Portion of label bold - the rest not bold
  • PayPal REST API cross reference transaction with payment
  • Why is the 100*0.07 equal to 6.9999…? [duplicate]
  • Braintree Dropin UI does not work with Ionic Framework unless force refresh
  • FIR filter in CUDA (as a 1D convolution)
  • How to get file download speed (transfer rate) with php?
  • Unique SMS sender id?
  • Yii2: Using Kartik Depdrop Widget?
  • How to Add Polymorphic Comments to Feed?
  • .NET video play library which allows to change the playback rate?
  • Xaml, wpf image position and crop issue
  • Getting different value with placeholder over CPU/GPU
  • Debugging VB6 Code From Visual Studio 2010
  • Selenium to click on a javascript button corresponding to a text
  • calculate gradient output for Theta update rule
  • Authentication in Play! and RestEasy
  • How do I configure context broker accept post requests from my remote sensor?
  • Diff between two dataframes in pandas
  • What's the purpose of QString?
  • Assign variable to the value in HTML
  • Alternative to overridePendingTransition() - Android
  • Blackberry - Custom EditField Cursor
  • Content-Length header not returned from Pylons response
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Is there a javascript serializer for JSON.Net?
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Where to put my custom functions in Wordpress?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Arrays break string types in Julia
  • Buffer size for converting unsigned long to string
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Does armcc optimizes non-volatile variables with -O0?
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF