/    Sign up×
Community /Pin to ProfileBookmark

JS Form – please help me with decimal places

Can anyone take a look at the following code and tell me how to get it to display the decimal place?

[code]
<!– hide this script tag’s contents from old browsers
function round2d(n)
{
return(Math.floor(100*n)/100);
}

function checkNumber(input, min, max, msg)
{
msg = msg + ” field has invalid data: ” + input.value;
var str = input.value;
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i + 1)
if ((ch < “0” || “9” < ch) && ch != ‘.’) {
alert(msg);
return false;
}
}
var num = 0 + str
if (num < min || max < num) {
alert(msg + ” not in range [” + min + “..” + max + “]”);
return false;
}
input.value = str;
return true;
}

function computeField(input)
{
if (input.value != null && input.value.length != 0)
input.value = “” + eval(input.value);
computeForm(input.form);
}


function computeForm(form)
{
if ((form.hours_complete.value == null || form.hours_complete.value.length == 0)) {
return;
}
if (!checkNumber(form.hours_complete, .5, 120, “Hours”)) {
form.gradepoints.value = “Invalid”;
return;
}
if ((form.hours_attempt.value == null || form.hours_attempt.value.length == 0)) {
return;
}
form.rate.value = (round2d(form.hours_complete.value / form.hours_attempt.value) * 100);
}

function clearForm(form)
{
form.hours_complete.value = “”;
form.rate.value = “”;
form.hours_attempt.value = “”;
}

//–>
</script>
[/code]

Edited by site staff: Inserted code tags.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumNov 20.2019 — You better use toFixed in order to get the decimal places:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
×

Success!

Help @lyounkins spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...