Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-07-2012, 02:11 PM   PM User | #1
dobbies
New to the CF scene

 
Join Date: Jun 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
dobbies is an unknown quantity at this point
How can I Round up numbers in this conversion table

I am using this code to convert liquid volume but it converts too many decimal places. Could someone show me how to reduce the conversion to 1 or 2 decimal places?

Many thanks

// JavaScript Document
<!--

function nofocus()
{
document.convert.InUnit.focus()
}
var mlValue = 1
var ltrValue = 1000
var fozValue = 28.4
var cupValue = 236
var ptValue = 568.261

function toCM()
{
var i = document.convert.unit.selectedIndex
var thisUnit = document.convert.unit.options[i].value
if (thisUnit == "ML")
{
document.convert.ml.value = document.convert.InUnit.value
}

else if(thisUnit == "LTR")
{
document.convert.ml.value = document.convert.InUnit.value * ltrValue
}

else if(thisUnit == "FOZ" )
{
document.convert.ml.value = document.convert.InUnit.value * fozValue
}

else if(thisUnit == "CUP" )
{
document.convert.ml.value = document.convert.InUnit.value * cupValue
}

else if(thisUnit == "PT" )

{
document.convert.ml.value = document.convert.InUnit.value * ptValue
}


toAll()
}
function toAll()
{
var m = document.convert.ml.value
document.convert.ltr.value = m / ltrValue
document.convert.foz.value = m / fozValue
document.convert.pt.value = m / ptValue
document.convert.cup.value = m / cupValue
}

//-->
dobbies is offline   Reply With Quote
Old 10-07-2012, 03:13 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Try...
Code:
document.convert.ltr.value = (m / ltrValue).toFixed(2);
jmrker is offline   Reply With Quote
Old 10-08-2012, 12:14 PM   PM User | #3
dobbies
New to the CF scene

 
Join Date: Jun 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
dobbies is an unknown quantity at this point
Quote:
Originally Posted by jmrker View Post
Try...
Code:
document.convert.ltr.value = (m / ltrValue).toFixed(2);
The above worked perfectly, many thanks for your time and help.

Cheers!
dobbies is offline   Reply With Quote
Old 10-08-2012, 12:27 PM   PM User | #4
shyagrawal
New Coder

 
Join Date: Sep 2012
Posts: 22
Thanks: 0
Thanked 6 Times in 6 Posts
shyagrawal is an unknown quantity at this point
you can also use
DecimalFormat df = new DecimalFormat("0.00");
shyagrawal is offline   Reply With Quote
Old 10-08-2012, 03:39 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by shyagrawal View Post
you can also use
DecimalFormat df = new DecimalFormat("0.00");
No. that is Java, not Javascript.

As well as toFixed() which results in a string value you can use

Code:
<script type = "text/javascript">

roundNumber = function(num, dec) {
return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
}

alert (roundNumber(123.45678,2));

</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-08-2012 at 03:41 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:34 AM.


Advertisement
Log in to turn off these ads.