lance22
asked on
Round parseFloat number value to two deciml points
I have a function that does some math, and am using parseFloat. The value it returns must be rounded to two decimal places. I have tried Math.Round(value) but it returns nothing. Is this because math.round does not work with parseFloat? The script uses two fields, and is as follows:
function totalFields()
{
myTotal1 = document.myForm.before.val ue;
myTotal2 = document.myForm.after.valu e;
myBigTotal = [(parseFloat(myTotal1)-par seFloat(my Total2))*1 00]/parseF loat(myTot al1);
return(myBigTotal)
}
function addemup()
{
document.myForm.grandtotal .value=tot alFields() ;
}
How can I convert the value to something recognized by math.round(value), or is there another way of converting this to two decimal points? A test value could be 12.55658334973432
Thanks in advance,
lance22
JavaScript Noob
function totalFields()
{
myTotal1 = document.myForm.before.val
myTotal2 = document.myForm.after.valu
myBigTotal = [(parseFloat(myTotal1)-par
return(myBigTotal)
}
function addemup()
{
document.myForm.grandtotal
}
How can I convert the value to something recognized by math.round(value), or is there another way of converting this to two decimal points? A test value could be 12.55658334973432
Thanks in advance,
lance22
JavaScript Noob
or
<script>
function convert(str){
str=str.match(/^\d*\.\d{2} /)
return str
}
</script>
<<form>
<p><input type="text" name="t1">
<input type="button" value="convert" onclick="t1.value=convert( t1.value)" > </p>
</form>
<script>
function convert(str){
str=str.match(/^\d*\.\d{2}
return str
}
</script>
<<form>
<p><input type="text" name="t1">
<input type="button" value="convert" onclick="t1.value=convert(
</form>
for your function do this
function totalFields()
{
myTotal1 = document.myForm.before.val ue;
myTotal2 = document.myForm.after.valu e;
myBigTotal = [(parseFloat(myTotal1)-par seFloat(my Total2))*1 00]/parseF loat(myTot al1);
myBigTotal=myBigTotal+' '
myBigTotal=myBigTotal.matc h(/^\d*\.\ d{2}/)
return(myBigTotal)
}
function addemup()
{
document.myForm.grandtotal .value=tot alFields() ;
}
function totalFields()
{
myTotal1 = document.myForm.before.val
myTotal2 = document.myForm.after.valu
myBigTotal = [(parseFloat(myTotal1)-par
myBigTotal=myBigTotal+' '
myBigTotal=myBigTotal.matc
return(myBigTotal)
}
function addemup()
{
document.myForm.grandtotal
}
hi lance22,
try this:
=======================
function totalFields()
{
myTotal1 = document.myForm.before.val ue*1;
myTotal2 = document.myForm.after.valu e*1;
return Math.round(((myTotal1-myTo tal2)*100) /myTotal1) ;
}
try this:
=======================
function totalFields()
{
myTotal1 = document.myForm.before.val
myTotal2 = document.myForm.after.valu
return Math.round(((myTotal1-myTo
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
After all that I think this is probably the most natural no ned for parseFloat, multiplying a string variable returns a number so you can rearrange the expression to return the correct number
function totalFields()
{
myTotal1 = document.myForm.before.val ue;
myTotal2 = document.myForm.after.valu e;
myBigTotal = (myTotal1*100-myTotal2*100 )/myTotal1 ;
myBigTotal=Math.round(100* myBigTotal )/100
return(myBigTotal)
}
function totalFields()
{
myTotal1 = document.myForm.before.val
myTotal2 = document.myForm.after.valu
myBigTotal = (myTotal1*100-myTotal2*100
myBigTotal=Math.round(100*
return(myBigTotal)
}
Nice and simple, just use this in place of Math.round;
function roundFloat(fltValue) {
return Math.round(fltValue * 100) / 100;
}
As per your example;
fltValue = 12.55658334973432
12.55658334973432 * 100 = 1255.658334973432
Math.round(1255.6583349734 32) = 1255
1255 / 100 = 12.55
So the function would output 12.55.
function roundFloat(fltValue) {
return Math.round(fltValue * 100) / 100;
}
As per your example;
fltValue = 12.55658334973432
12.55658334973432 * 100 = 1255.658334973432
Math.round(1255.6583349734
1255 / 100 = 12.55
So the function would output 12.55.
Thats what I just said
I was just providing an OO solution. A concept sadly lost on many people.
OO can not be implemeneted blindly without attention to the specifics of the problem. There are a number of type converison problems here
>> OO can not be implemeneted blindly without attention to the specifics of the problem.
I hardly think this is a blind implementation.
>> There are a number of type converison problems here
Including the error in your solution?
I hardly think this is a blind implementation.
>> There are a number of type converison problems here
Including the error in your solution?
What error?
ps
Math.round(1255.6583349734 32) = 1256 not 1255
so the output would be
1256 / 100 = 12.56
ps
Math.round(1255.6583349734
so the output would be
1256 / 100 = 12.56
Quite the flaming down please
You are hurting my ears
You are hurting my ears
I agree. Just in case my proposed solution does not get lost in the above I suggest changing totalFields() to
function totalFields()
{
myTotal1 = document.myForm.before.val ue;
myTotal2 = document.myForm.after.valu e;
myBigTotal = (myTotal1*100-myTotal2*100 )/myTotal1 ;
myBigTotal=Math.round(100* myBigTotal )/100
return(myBigTotal)
}
function totalFields()
{
myTotal1 = document.myForm.before.val
myTotal2 = document.myForm.after.valu
myBigTotal = (myTotal1*100-myTotal2*100
myBigTotal=Math.round(100*
return(myBigTotal)
}
ps
Math.round(1255.6583349734 32) = 1256 not 1255
so the output would be
1256 / 100 = 12.56
-- Ah thanks dude, damn hope my code doesn't do that. :)
Math.round(1255.6583349734
so the output would be
1256 / 100 = 12.56
-- Ah thanks dude, damn hope my code doesn't do that. :)
Heres another nice little example in the spirit of OO design;
function roundFloat(fltValue, intDecimal) {
return Math.round(fltValue * Math.pow(10, intDecimal)) / Math.pow(10, intDecimal)
}
Not quite as simple as the other examples but this one lets you round to any number of decimal places. :) Just specify the number to be rounded (fltValue) and the number of decimal places (intDecimal).
function roundFloat(fltValue, intDecimal) {
return Math.round(fltValue * Math.pow(10, intDecimal)) / Math.pow(10, intDecimal)
}
Not quite as simple as the other examples but this one lets you round to any number of decimal places. :) Just specify the number to be rounded (fltValue) and the number of decimal places (intDecimal).
lance22,
Not trying to clutter the thread, but after taking advice from the above comments which are needed for compatibility with older browsers, you can also make note of a built-in function that works on newer browsers:
var myNumber=1.222367;//exampl e
alert(myNumber.toFixed(2)) ;
the toFixed function is in Javascript 1.5
Not trying to clutter the thread, but after taking advice from the above comments which are needed for compatibility with older browsers, you can also make note of a built-in function that works on newer browsers:
var myNumber=1.222367;//exampl
alert(myNumber.toFixed(2))
the toFixed function is in Javascript 1.5
ASKER
I appreciate everyone's help very much. My wife had suggested .toFixed but this stuff will be used by people with OLD computers. Thank you.
the solution of regular expression doesn't work for decimal ranging between 0 and 1
num='234.123453'
str=new String()
str=num
str=str.match(/^\d*\.\d{2}
alert(str)
</script>