Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the Increment Value of HTML Number Input - Decimals

Tags:

html

Is there any way to change how much a number is incremented when using the up/down arrows on a HTML number input form?

<input type="number" step="any" value="0.00000000"></input> 

I'm working with tiny numbers, it would be nice if the up/down arrows incremented just 0.00000001 at a time, instead of a whole number.

  • 0.00000000
  • 0.00000001
  • 0.00000002
  • 0.00000003

Instead of

  • 0.00000000
  • 1
  • 2
  • 3

I doubt very much if there is an easy way to do this, just though I'd ask and see if anyone else has a workaround or method as I'm sure many people experience a similar issue.

Thanks,

like image 507
Ryan Avatar asked Sep 23 '14 19:09

Ryan


People also ask

How do I put 2 decimal places in HTML?

Use the toFixed() method to format a number to 2 decimal places, e.g. num. toFixed(2) . The toFixed method takes a parameter, representing how many digits should appear after the decimal and returns the result. Copied!

How do I limit the number of decimal places in HTML?

To limit, that is, round a number to n decimal places, use the built-in Math. round() function.

How do you increment numbers in HTML?

The stepUp() method increments the value of the number field by a specified number. Tip: To decrement the value, use the stepDown() method.

How do you set the value of an input type number?

The <input type="number"> defines a field for entering a number. Use the following attributes to specify restrictions: max - specifies the maximum value allowed. min - specifies the minimum value allowed.


1 Answers

The step attribute is for this. Your code now has the value any, which means that any values are accepted and the step size is the default, 1. Replace it by a specific number to set the granularity and the step size. Example:

<input type="number" step="0.00000001" value="0.00000000">

Note: The end tag </input> is invalid in HTML syntax for HTML5. In XHTML syntax it is allowed, but “self-closing tags” are recommended instead of it, e.g. <input type="number" step="0.00000001" value="0.00000000" />.

like image 183
Jukka K. Korpela Avatar answered Sep 22 '22 11:09

Jukka K. Korpela