The number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by step).
Simply change this value to whatever is appropriate. For money, two decimal places are probably expected:
<label for="totalAmt">Total Amount</label>
<input type="number" step="0.01" id="totalAmt">
(I'd also set min=0 if it can only be positive)
If you'd prefer to allow any number of decimal places, you can use step="any" (though for currencies, I'd recommend sticking to 0.01). In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any. (thanks to Michal Stefanow's answer for pointing out any, and see the relevant spec here)
Here's a playground showing how various steps affect various input types:
<form>
<input type=number step=1 /> Step 1 (default)<br />
<input type=number step=0.01 /> Step 0.01<br />
<input type=number step=any /> Step any<br />
<input type=range step=20 /> Step 20<br />
<input type=datetime-local step=60 /> Step 60 (default)<br />
<input type=datetime-local step=1 /> Step 1<br />
<input type=datetime-local step=any /> Step any<br />
<input type=datetime-local step=0.001 /> Step 0.001<br />
<input type=datetime-local step=3600 /> Step 3600 (1 hour)<br />
<input type=datetime-local step=86400 /> Step 86400 (1 day)<br />
<input type=datetime-local step=70 /> Step 70 (1 min, 10 sec)<br />
</form>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user. You must also validate on the server-side!
Answer from Dave on Stack OverflowThe number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by step).
Simply change this value to whatever is appropriate. For money, two decimal places are probably expected:
<label for="totalAmt">Total Amount</label>
<input type="number" step="0.01" id="totalAmt">
(I'd also set min=0 if it can only be positive)
If you'd prefer to allow any number of decimal places, you can use step="any" (though for currencies, I'd recommend sticking to 0.01). In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any. (thanks to Michal Stefanow's answer for pointing out any, and see the relevant spec here)
Here's a playground showing how various steps affect various input types:
<form>
<input type=number step=1 /> Step 1 (default)<br />
<input type=number step=0.01 /> Step 0.01<br />
<input type=number step=any /> Step any<br />
<input type=range step=20 /> Step 20<br />
<input type=datetime-local step=60 /> Step 60 (default)<br />
<input type=datetime-local step=1 /> Step 1<br />
<input type=datetime-local step=any /> Step any<br />
<input type=datetime-local step=0.001 /> Step 0.001<br />
<input type=datetime-local step=3600 /> Step 3600 (1 hour)<br />
<input type=datetime-local step=86400 /> Step 86400 (1 day)<br />
<input type=datetime-local step=70 /> Step 70 (1 min, 10 sec)<br />
</form>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user. You must also validate on the server-side!
Via: http://blog.isotoma.com/2012/03/html5-input-typenumber-and-decimalsfloats-in-chrome/
But what if you want all the numbers to be valid, integers and decimals alike? In this case, set step to “any”
<input type="number" step="any" />
Works for me in Chrome, not tested in other browsers.
How to make python accept both int and float input???
How would I make a program only accept floats and integers?
Take Float input with number_input
Float input is apparently wrong
If your inputs are "sometimes" ints and "sometimes" floats then just wrap each input in a float(). You could make something more complex, but why would you?
You could check for the presence of a decimal point in your string to decide if you want to coerce it into a float or an int.
number = input()
if '.' in number:
number = float(number)
else:
number = int(float(number))
So I have a program that has an input variable like ‘money = float(input(“How much money do you have?”)) ‘ I want to know in case I make a typo and add a string like inputting “5t6” I want the program to ask the question “please input a float” instead of just crashing. What would I do for that?
Hi, I've posted here recently and I'm sure I will again. The site myself course is on seems ridiculously made, and makes me question existence.
Part of a question reads "Read a float from the keyboard and assign it to a variable named d. (Do not print a prompt. Use the input() function without a prompt string to read the input)
My interpretation was to type d = float(input('Type a float ')) 87.5
It's a site called Pearson Revel if anyone else took a similar class, btw. Anyway, it says my program is wrong because "Correct solutions that use float tend to also use ___". The statement ends with nothing and doesn't say what's wrong. Any help would be greatly appreciated, and thanks in advance!
EDIT - I tried to use no prompt and it still marked it wrong. Actually, it marked more flaws in the program w/o a prompt.
My current code accepts integers that are greater than 0, which is what I want, but I also want to accept floats/decimals as well. How would I do this? Here is my current code:
while True:
try:
time = int(input("How much time has elapsed since your last stop (in hours)?"))
except ValueError:
print("Enter a valid number of hours!")
continue
if time > 0:
givenTime[2] = time
breakIndentation is off here for some reason, but believe me when I say that the indentation in the actual code is correct..
Can I just do time = int or float(input("How much time has elapsed since your last stop (in hours)?")) or would this be wrong? Any help would be appreciated.
edit: nope,
time = int or float(input("How much time has elapsed since your last stop (in hours)?"))
doesn't work.
I don't understand why you don't just use floats?
while True:
try:
time = float(raw_input("How much time has elapsed since your last stop (in hours)?"))
except ValueError:
print("Enter a valid number of hours!")
continue
By typing an integer value, it'll just turn it into a float anyway.
here's something I wrote a few months ago for a similar question. Figure out how to modify it to do what you want:
def input_type(msg, type_converter):
while True:
s = input(msg)
try :
return type_converter(s)
except ValueError as e:
print(e)
def input_lessthan(msg, maximum, type_converter=int):
while True:
n = input_type(msg, type_converter)
if n <= maximum:
return n
print("Please enter a number less than {}".format(maximum))
print(input_lessthan("hours worked> ", 168, float))