Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need React input box to be a floating number [duplicate]

I need a simple input box to be a floating point number (anything with a . really). As it is right now, it doesn't allow me to put the dot and do a floating point. Should be simple but I can't find much on this.

Here is roughly the current code:

class MyComponent extends React.Component {
  render () {
    return (
      <td>
        <label> Value: </label>
            <input
                type='number'
                min='0'
                max='20'
                className='form-control'
                value={this.state.value}
                onChange= {(evt) => this.onValueChange('value', evt.target.value)}
              />
      </td>
    )
  }
}
like image 984
theJuls Avatar asked Feb 12 '18 14:02

theJuls


1 Answers

The number type has a step value controlling which numbers precision which defaults to 1.

You can use this to set the floating point precision

<input type="number" step="0.1">

You will have

class MyComponent extends React.Component {
  render () {
    return (
      <td>
        <label> Value: </label>
            <input
                type='number'
                step="0.1"
                min='0'
                max='20'
                className='form-control'
                value={this.state.value}
                onChange= {(evt) => this.onValueChange('value', evt.target.value)}
              />
      </td>
    )
  }
}
like image 198
Shubham Khatri Avatar answered Sep 29 '22 05:09

Shubham Khatri