/    Sign up×
Community /Pin to ProfileBookmark

I need only 2 decimal place

For an instant, under “GRT” if I were to key in 45578, instantly under “Port Dues” would be 1002.716(the result is as such due to a formula).

The thing is, instead of 1002.716, I want the result to be in two decimal place only, e.g, 1002.71 instead.

What would be the function be?

Link -> [url]http://www.oceanbulk.com.sg/mat.html[/url]

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Jul 26.2004 — Try the roundTo() prototype:<script type="text/javascript">

Number.prototype.roundTo = function(d) {
return (Math.round(this * Math.pow(10,d)) / Math.pow(10,d))
}

var num = 215.561
alert(num.roundTo(0))
alert(num.roundTo(1))
alert(num.roundTo(2))

</script>
Dr. Script
Copy linkTweet thisAlerts:
@Mr_JJul 26.2004 — You could also use


var num = 215.561

alert(num.toFixed(2))
Copy linkTweet thisAlerts:
@steelersfan88Jul 26.2004 — but the toFixed() prototype returns a string
Copy linkTweet thisAlerts:
@Mr_JJul 26.2004 — You learn something new everyday ?

Thanks
Copy linkTweet thisAlerts:
@oceanmaidauthorJul 27.2004 — I don't know what you two guys talking about...In anyway, it's not working at all. Could you insert the function for me and send me the whole script? Gee, thanks....:p
Copy linkTweet thisAlerts:
@steelersfan88Jul 27.2004 — Actually, they both work. Please provide the code you are using. Neither Mr J nor I can guess you you want this to be implemented, but we both gave prototypes that will return rounded results.

We both also provided at least one example of how to use both methods to round a number.

Dr. Script
Copy linkTweet thisAlerts:
@steelersfan88Jul 27.2004 — By the way, this is the whole script:[code=php]<script type="text/javascript">

Number.prototype.roundTo = function(d) {
return (Math.round(this * Math.pow(10,d)) / Math.pow(10,d))
}

var num = 215.561
alert(num.roundTo(0))
alert(num.roundTo(1))
alert(num.roundTo(2))

</script>[/code]
You round it by adding the [font=monospace]roundTo([i]dec_places[/i])[/font]. This will not change the variable to be rounded, then you'd have to do: //JavaScript:
var num = 215.561
num = num.roundTo(2)
Dr. Script
Copy linkTweet thisAlerts:
@oceanmaidauthorJul 28.2004 — It's obviously not working...

C for your self...?

http://www.oceanbulk.com.sg/mat.html
Copy linkTweet thisAlerts:
@steelersfan88Jul 28.2004 — You didn't even put the code in ... how will it work without the code?
Copy linkTweet thisAlerts:
@oceanmaidauthorJul 28.2004 — Where exactly should I put the code?
Copy linkTweet thisAlerts:
@steelersfan88Jul 28.2004 — Along the lines of:[code=php] document.getElementById('amount1').value=Number(document.getElementById('a').value)*Number(document.getElementById('unit').value).roundTo(2);
document.getElementById('amount2').value=Number(document.getElementById('b').value)*Number(document.getElementById('unit1').value).roundTo(2);
document.getElementById('amount3').value=Number(document.getElementById('c').value)*Number(document.getElementById('unit2').value).roundTo(2);
document.getElementById('amount4').value=Number(document.getElementById('d').value)*Number(document.getElementById('unit3').value).roundTo(2);
document.getElementById('amount5').value=Number(document.getElementById('e').value)*Number(document.getElementById('unit4').value).roundTo(2);
document.getElementById('amount6').value=Number(document.getElementById('f').value)*Number(document.getElementById('unit5').value).roundTo(2);
document.getElementById('amount7').value=Number(document.getElementById('g').value)*Number(document.getElementById('unit6').value).roundTo(2);
document.getElementById('amount8').value=Number(document.getElementById('h').value)*Number(document.getElementById('unit7').value).roundTo(2);
document.getElementById('amount9').value=Number(document.getElementById('i').value)*Number(document.getElementById('unit8').value).roundTo(2);
document.getElementById('amount10').value=Number(document.getElementById('j').value)*Number(document.getElementById('unit9').value).roundTo(2);
document.getElementById('amount11').value=Number(document.getElementById('k').value)*Number(document.getElementById('unit10').value).roundTo(2);
document.getElementById('amount12').value=Number(document.getElementById('l').value)*Number(document.getElementById('unit11').value).roundTo(2);[/code]
Then add://JavaScript
Number.prototype.roundTo = function(d) {
return (Math.round(this * Math.pow(10,d)) / Math.pow(10,d))
}
Dr. Script
Copy linkTweet thisAlerts:
@oceanmaidauthorJul 29.2004 — I can't understand why..but obviously, it's not working, though.?
Copy linkTweet thisAlerts:
@NedalsJul 29.2004 — Change the lines of javascript to this (with appropiate id's)

document.getElementById('amount1').value=(document.getElementById('a').value * document.getElementById('unit').value).toFixed(2);
×

Success!

Help @oceanmaid 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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