6

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>
    )
  }
}
theJuls
  • 6,788
  • 14
  • 73
  • 160

1 Answers1

9

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>
    )
  }
}
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400