Hi Everyone,
I have a requirement of formatting the text inside the input text box. On page load, the textbox value by default get set to say 10.0 or 15.0. What I want is just integer and remove the decimal point and the values ahead.

Thanks in Advance.

Regards
JJ

Dani AI

Generated

Short answer for : CSS cannot change the actual text/value stored in an input element — it only controls presentation. The decimal-format/number-format ideas won’t strip characters from the input value itself. As pointed out, the first decision is whether to truncate (drop fractional part) or to round, because that choice changes the result for non-integer inputs.

Practical options (ordered by reliability):

  • Format on the server before rendering the page so the input starts with an integer.
  • Use HTML5 constraints to help users: type="number" with step="1" and a pattern will guide/validate input but will not automatically remove decimals when typed.
  • Use a tiny client-side script to clean the value on load, on blur, or just before submit — this is the simplest way to guarantee the value is an integer in the browser.

Minimal client-side examples (truncation vs rounding):

<script>
function stripDecimals(value){
  return value.replace(/[.,]\d+$/, '');
}

function roundToInteger(value){
  var n = Number(value.replace(',', '.'));
  return isNaN(n) ? value : String(n.toFixed(0));
}

document.addEventListener('DOMContentLoaded', function(){
  var inp = document.getElementById('amount'); // change selector to match
  if(!inp) return;
  inp.value = stripDecimals(inp.value); // initial cleanup
  inp.addEventListener('blur', function(){
    this.value = stripDecimals(this.value); // or use roundToInteger(this.value)
  });
});
</script>

Notes and cautions: handle localization (comma vs period decimals) before numeric conversion, and always enforce the integer rule server-side too — client-side fixes are for UX only. If JavaScript is not allowed, formatting must happen before the page is sent (server-side) because there’s no pure-CSS way to alter the input’s value.

Recommended Answers

All 8 Replies

Should 10.6 become 10 or 11?

10.0-> 10

Yes, but should non-integers be rounded or floor'd? If rounded, use Math.round(element.value) , if not, use parseInt(element.value) .

I want to achieve this using only css. No javascript. only css. Is that possible ?

Nope. You can't alter an input value from CSS.

there is something cal l@decimal-format / number-format in css. But somehow its not working.

Yes, but that applies to the content, not the value of an element.

Can you post the code of the input element, and the css you have tried?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.