|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello all,
I have a web form that has an external javascript file attached to it. There is a field called monthlyPremium, which the user will put in their monthly premium (decimals may be needed) and then beside it is a annualPremium field that takes the monthlyPremium data, and multiplies it by 12. It works fine, unless I put in a value such as 425.65, I get 5107.799999999999 back as the answer, instead of 5107.80. The javascript I have to calculate that is this: Code:
function fillShenandoah()
{
var monthlyPremium = document.weeklyProduction.shenandoahPremium.value
var months = "12"
var annualPremium = parseFloat(monthlyPremium * months)
document.weeklyProduction.shenandoahAnnualPremium. value = annualPremium
}
Now, is there a javascript I can put in, to round it to 2 decimal places? Thanks. ![]() |
|
#2
|
||||
|
||||
|
Here's a function that will round to certain decimal points:
Code:
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
Then use it something like this: Code:
var roundedNumber = roundNumber(annualPremium,2)
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter! DevArticles Forum Moderator "The net is a waste of time, and that's exactly what's right about it." -- William Gibson |
|
#3
|
|||
|
|||
|
Thanks
Thanks.
The code you had on there before you edited worked great. ![]() Code:
var result = Math.round(annualPremium*100)/100 I just set the variable, and then set my document.blah.blah.value = to that, after setting the annualPremium = to the result. ![]() That other code looks great for rounding to different decimal places. ![]() Thanks again ![]() |
|
#4
|
||||
|
||||
|
lol, bah... you watched the edits! =P
I modified it to make it a little more global. ![]() |
|
#5
|
|||
|
|||
|
This code does not work
check out thie exampe Code:
<html>
<head>
</head>
<body>
<script type="text/javascript">
function roundVal(val){
var dec = 2;
var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
var value = 613.305;
document.write(roundVal(value));
</script>
</body>
</html>
The code above produces a result of 613.30 but it should clearly be 613.31 |
|
#6
|
||||
|
||||
|
ill try and create something to round to a certian spot after an input for the digit using strings and substrings with parseing, it shouldnt be that hard but ill go for it.
![]() colton22 |
|
#7
|
||||
|
||||
|
This is how far i have gotten so far...
Code:
<SCRIPT>
function fixedRound(num2round,digits) {
var num=num2round.toString();
if (num.length<=digits) { //>
var sep=num.indexOf(".");
if (sep==-1) {num+=".";sep=num.indexOf(".");}
var dec=num.substring(sep,num.length);
for (var x=0;x<eval((digits-dec.length)+1);x++)/*>*/{num=num+"0";}
return num;
}
else {
var sep=num.indexOf(".");
sep=sep+digits+1;
num=num.substring(0,sep);
if (parseInt(num.substring(eval(sep-1),sep))>=5) {movelastnum=true;}else {movelastnum=false;}
if (movelastnum) {var numbers=new Array();
for (var x=0;x<num.length;x++)/*>*/{numbers[numbers.length]=num.substring(x,eval(x+1));}
var long=numbers.length;
if (numbers[long]!=9) {numbers[long]=numbers[long]+1;}
else {
for(var y=long;y>0;y--) {
if (numbers[y]!=".");
if (parseInt(numbers[y])==9) {numbers[y]="0";}
else {numbers[y]=parseInt(numbers[y]);+1;}
}
}
for (var x=0;x<numbers.length;x++) {num+=numbers[x];}}
return num;
}
}
alert(fixedRound(prompt("num",""),parseInt(prompt("digits",""))));
</SCRIPT>
if anyone wants to use the above code and run with it go for it. otherwise i will still be working on it. colton22 |
|
#8
|
|||
|
|||
|
Quote:
This is almost certainly a problem with floats. JavaScript cannot store floats precisely, so it 613.305 will probably be stored as something like 613.305000000002, which is correctly rounded up. The only way to avoid this is to use integers and apply any decimal places at the display stage. |
|
#9
|
||||
|
||||
|
alright, i re-did it, i got it to round the example above, but it wont work on like rounding to 0, if anyone wants to critek this alright...
Code:
<SCRIPT>
function _roundNumber(num,dec) {
var snum=num.toString()+"000000000000000001";
var sep=snum.indexOf(".");
var beg=snum.substring(0,snum.indexOf("."));
snum=snum.substring(eval(snum.indexOf(".")+1),snum.length);
var dig=snum.substring(0,eval(dec-1));
snum=snum.substring(eval(dec-1),dec);
snum=parseInt(snum);
gohigher=false;
if (snum>4) {gohigher=true;}
if (gohigher) {snum=parseInt(snum);snum++;}
snum=snum.toString();
alert(beg+"."+dig+""+snum);
num=beg+"."+dig+""+snum;
return num;
}
alert(_roundNumber(prompt("num"),prompt("number of digits")));
</SCRIPT>
colton22
__________________
![]() Hang Out, Listen To Music, Have Fun, and Customize Your Experiance All In One Place! - Colton22's World - This is My World |
|
#10
|
|||
|
|||
|
Rounding to 2 decimal places.
Well i tried for it and found it to be quite simple.
myAmt = 234.4243 myAmt.toFixed(2) The result will be 234.42 |
|
#11
|
|||
|
|||
|
Quote:
.toFixed is NOT reliable! Try it with 0.285, and you will get 0.28 instead of 0.29. The ONLY way to reliably round numbers in Javascript is to parse the number as a string. It is crazy that there is no simple way of doing it, but there you go. I have written a function to do this, but I am not allowed to post a link to it here because I am a new user! I cannot just paste the code in here either because there is some licensing and contact info that goes with it. So visit netshinesoftware.com and click on the developer blog if you want it! |
|
#12
|
|||
|
|||
|
Hmmm, I got It!
Quote:
Try Using This..... Code:
function roundNumber(num, dec) {
var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec);
return result;
}
|
|
#13
|
|||
|
|||
|
simple math
There is no specific rule in math that says that .5 should be rounded up to 1.. .5 is just as far from 1 as from 0, therefore it is not a script problem. If you wish to round up by default, then just increase any var ending by .5 slightly.
|
|
#14
|
|||
|
|||
|
round to 2 decimal places.
very old thread I know..... but I like a puzzle (even a very old one).
Code:
function round(n) {
return Math.round(n*100+((n*1000)%10>4?1:0))/100;
}
test page.... Code:
<html>
<head>
<script type="text/javascript">
/**
* Rounds a number to two decimal places.
* 7.335 is rounded to (7.34) so 5/100 always does ceiling)
* @n the number to round (float type expected)
* @return a rounded number (you may need to pad this to 2 decial places)
*/
function round(n) {
return Math.round(n*100+((n*1000)%10>4?1:0))/100;
}
/**
* Test harness only. Performs nice decimal padding for us.
*/
function tryRound(ctl) {
var ctl = document.getElementById("txtDummy");
var val = ctl.value;
val = parseFloat(val);
if (!isNaN(val)) {
var n = round(val)+"";
if (n.indexOf(".")==-1) {
n += ".00";
}
while(n.length-n.indexOf(".")<3) {
n += "0";
}
ctl.value = n;
}
}
</script>
</head>
<body>
<form>
<input id="txtDummy" type="text" value="0"/>
<a href="#" onclick="tryRound(); return false;">round</a>
</form>
</html>
|
|
#15
|
|||
|
|||
|
Quote:
From what I've learned in math all the way up to university is that in cases where it ends with "5" and you need to round, you round to the nearest EVEN number. So 0.85 = 0.8 and 0.75 = 0.8. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Javascript to Round to 2 Decimal Places? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|