How to set float input type in HTML5?

HTML5 does not include a specific float input type, but there are effective ways to create input fields that accept floating-point (decimal) numbers. This is commonly needed for forms that collect data like CGPA scores, prices, measurements, or any numeric values requiring decimal precision.

What is Float Input?

A float or floating-point number is a number that contains a decimal point, such as 3.14, 9.75, or 100.50. Float inputs are essential when precision beyond whole numbers is required, such as in financial calculations, scientific measurements, or academic grading systems.

Approaches to Set Float Input Type

While HTML5 does not provide a dedicated "float" input type, we can achieve float input functionality using two primary approaches

  1. Using HTML inputmode Attribute
  2. Using HTML step Attribute

Using HTML inputmode Attribute

The inputmode="decimal" attribute provides a user-friendly way to input decimal numbers, especially on mobile devices. When this attribute is set, mobile browsers display a numeric keypad with decimal point access, making it easier for users to enter floating-point values.

Syntax

<input type="text" inputmode="decimal" pattern="[0-9]*[.,]?[0-9]*">

The pattern attribute uses a regular expression that accepts digits, optionally followed by a comma or decimal point and more digits. This ensures proper validation of decimal input.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Float Input with inputmode</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Student CGPA Form</h2>
   <form>
      <label for="cgpa">Enter CGPA (0.00-4.00):</label><br>
      <input type="text" id="cgpa" name="cgpa" 
             inputmode="decimal" 
             pattern="[0-9]*[.,]?[0-9]*" 
             placeholder="3.75" 
             style="padding: 8px; margin: 10px 0;"><br>
      <input type="submit" value="Submit" style="padding: 8px 15px;">
   </form>
</body>
</html>

The output displays a form with a text input optimized for decimal entry

Student CGPA Form
Enter CGPA (0.00-4.00):
[Text input field with decimal keypad on mobile]  [Submit]

Using HTML step Attribute

The step attribute with type="number" provides precise control over decimal input. Setting step="0.01" allows increments of 0.01, making it ideal for currency, percentages, or other measurements requiring two decimal places.

Syntax

<input type="number" step="0.01" min="0" max="1000">

The step attribute defines the increment value, while min and max set the acceptable range for the input.

Example Price Input

<!DOCTYPE html>
<html>
<head>
   <title>Float Input with step Attribute</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Product Price Form</h2>
   <form>
      <label for="price">Product Price ($):</label><br>
      <input type="number" id="price" name="price" 
             min="0" max="10000" step="0.01" 
             placeholder="99.99"
             style="padding: 8px; margin: 10px 0;"><br>
      <input type="submit" value="Add Product" style="padding: 8px 15px;">
   </form>
</body>
</html>

The output shows a number input with decimal precision and spinner controls

Product Price Form
Product Price ($):
[Number input with up/down arrows: 99.99]  [Add Product]

Example Scientific Measurement

Following example demonstrates float input for scientific measurements with higher precision

<!DOCTYPE html>
<html>
<head>
   <title>Scientific Measurement Input</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Lab Measurement Form</h2>
   <form>
      <label for="temperature">Temperature (°C):</label><br>
      <input type="number" id="temperature" name="temperature" 
             min="-100" max="200" step="0.1" 
             placeholder="23.5"
             style="padding: 8px; margin: 10px 0;"><br>
      
      <label for="weight">Weight (kg):</label><br>
      <input type="number" id="weight" name="weight" 
             min="0" max="1000" step="0.001" 
             placeholder="15.750"
             style="padding: 8px; margin: 10px 0;"><br>
      
      <input type="submit" value="Record Data" style="padding: 8px 15px;">
   </form>
</body>
</html>

This form allows precise decimal input for scientific measurements with different step values.

Comparison of Methods

Following table compares the two approaches for creating float inputs

inputmode="decimal" step Attribute
Uses type="text" with decimal keypad Uses type="number" with numeric controls
Better mobile user experience Built-in validation and spinner controls
Requires pattern attribute for validation Automatic numeric validation
Accepts any decimal format Enforces specific step increments
More flexible input parsing Precise control over decimal places

Validation with JavaScript

For enhanced validation, you can combine HTML5 attributes with JavaScript validation

<!DOCTYPE html>
<html>
<head>
   <title>Float Input with Validation</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Validated Float Input</h2>
   <form onsubmit="return validateFloat()">
      <label for="amount">Amount:</label><br>
      <input type="number" id="amount" name="amount" 
             step="0.01" min="0" 
             placeholder="Enter decimal number"
             style="padding: 8px; margin: 10px 0;"><br>
      <input type="submit" value="Validate" style="padding: 8px 15px;">
      <p id="message" style="color: red;"></p>
   </form>
   
   <script>
      function validateFloat() {
         const input = document.getElementById("amount");
         const value = parseFloat(input.value);
         const message = document.getElementById("message");
         
         if (isNaN(value) || value < 0) {
            message.textContent = "Please enter a valid positive number.";
            return false;
         }
         
         message.textContent = "Valid float: " + value.toFixed(2);
         message.style.color = "green";
         return false; // Prevent actual submission for demo
      }
   </script>
</body>
</html>

This example provides real-time validation feedback for float input values.

Conclusion

While HTML5 lacks a dedicated float input type, the inputmode="decimal" approach offers excellent mobile usability, while the step attribute with type="number" provides precise decimal control and validation. Choose the method that best fits your form's requirements and user experience goals.

Updated on: 2026-03-16T21:38:54+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements