|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#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 |
![]() |
| 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 |
|
|
|
|