I need to round the number of a field
I have tried many things about the ro开发者_Go百科unding of a filed but I get NaN, here is what I am trying to round up to 999999,9
document.forms[0].NB_CONCN_MOY_DCO_MS
I have tried Math.round(document.forms[0].NB_CONCN_MOY_DCO_MS.value)
and
document.forms[0].Math.round(NB_CONCN_MOY_DCO_MS.value)
what can I do now.
If you want to round a number to one decimal place in JavaScript, use someNumber.toFixed(1)
. Note that the value
of form fields is a string (not a number) so you'll want to convert it to a number first.
var n = document.forms[0].NB_CONCN_MOY_DCO_MS.value * 1;
var rounded = n.toFixed(1);
If your value has commas in it to represent decimal values, you will need to fix the string to use periods instead first:
var n = document.forms[0].NB_CONCN_MOY_DCO_MS.value.replace(/,/,',') * 1;
var rounded = n.toFixed(1);
精彩评论