Solved

Round parseFloat number value to two deciml points

Posted on 2003-12-08
19
11,518 Views
Last Modified: 2012-06-21
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.value;
myTotal2 = document.myForm.after.value;
myBigTotal = [(parseFloat(myTotal1)-parseFloat(myTotal2))*100]/parseFloat(myTotal1);
return(myBigTotal)
}
function addemup()
{
document.myForm.grandtotal.value=totalFields();
}

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
0
Comment
Question by:lance22
19 Comments
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9900663
<script>
num='234.123453'
str=new String()
str=num
str=str.match(/^\d*\.\d{2}/)
alert(str)
</script>

0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9900676
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>
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9900699
for your function do this

function totalFields()
{
myTotal1 = document.myForm.before.value;
myTotal2 = document.myForm.after.value;
myBigTotal = [(parseFloat(myTotal1)-parseFloat(myTotal2))*100]/parseFloat(myTotal1);

myBigTotal=myBigTotal+' '
myBigTotal=myBigTotal.match(/^\d*\.\d{2}/)
return(myBigTotal)
}
function addemup()
{
document.myForm.grandtotal.value=totalFields();
}
0
 
LVL 25

Expert Comment

by:devic
ID: 9900819
hi lance22,

try this:
=======================
function totalFields()
{
      myTotal1 = document.myForm.before.value*1;
      myTotal2 = document.myForm.after.value*1;
      return Math.round(((myTotal1-myTotal2)*100)/myTotal1);
}
0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 250 total points
ID: 9900822
oops that truncates

<script>
function convert(str){
str=str.match(/^\d*\.\d{3}/)*100
str=Math.round(str)/100
return str
}
</script>

<form>
  <p><input type="text" name="t1">
<input type="button" value="convert" onclick="t1.value=convert(t1.value)"> </p>
</form>

so your function is

function totalFields()
{
myTotal1 = document.myForm.before.value;
myTotal2 = document.myForm.after.value;
myBigTotal = [(parseFloat(myTotal1)-parseFloat(myTotal2))*100]/parseFloat(myTotal1);

myBigTotal=myBigTotal+' '
myBigTotal=myBigTotal.match(/^\d*\.\d{3}/)*100
myBigTotal=Math.round(myBigTotal)/100
return(myBigTotal)
}


0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9900868
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.value;
myTotal2 = document.myForm.after.value;
myBigTotal = (myTotal1*100-myTotal2*100)/myTotal1;
myBigTotal=Math.round(100*myBigTotal)/100
return(myBigTotal)
}
0
 
LVL 1

Expert Comment

by:Jagata
ID: 9901563
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.658334973432) = 1255
1255 / 100 = 12.55

So the function would output 12.55.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9901586
Thats what I just said
0
 
LVL 1

Expert Comment

by:Jagata
ID: 9901620
I was just providing an OO solution. A concept sadly lost on many people.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9901768
OO can not be implemeneted blindly without attention to the specifics of the problem. There are a number of type converison problems here
0
 
LVL 1

Expert Comment

by:Jagata
ID: 9901779
>> 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?
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9901838
What error?

ps

Math.round(1255.658334973432) = 1256  not 1255

so the output would be

1256 / 100 = 12.56
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9901901
Quite the flaming down please

You are hurting my ears
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9901912
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.value;
myTotal2 = document.myForm.after.value;
myBigTotal = (myTotal1*100-myTotal2*100)/myTotal1;
myBigTotal=Math.round(100*myBigTotal)/100
return(myBigTotal)
}
0
 
LVL 1

Expert Comment

by:Jagata
ID: 9901965
ps

Math.round(1255.658334973432) = 1256  not 1255

so the output would be

1256 / 100 = 12.56

-- Ah thanks dude, damn hope my code doesn't do that. :)
0
 
LVL 1

Expert Comment

by:Jagata
ID: 9902001
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).
0
 
LVL 12

Expert Comment

by:ahosang
ID: 9903489
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;//example
alert(myNumber.toFixed(2));

the toFixed function is in Javascript 1.5
0
 

Author Comment

by:lance22
ID: 9905407
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.
0
 

Expert Comment

by:kajalg
ID: 10208380
the solution of regular expression doesn't work for decimal ranging between 0 and 1
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now