?
Solved

rounding number to two decimal places not working

Posted on 2011-09-16
6
Medium Priority
?
404 Views
Last Modified: 2012-05-12
In the attached code, based on values on my screen, the variable is spitting out:

286.88524590163934

I've searched extensively on the web for this, everyone says just put a toFixed(2) at the end, but it isn't working for me, for some reason.

Thank you!
var weight2a=((parseFloat($('#weight1b').val())*3500))/parseInt(days_between(weight2aBeginDate, weight2aEndDate)).toFixed(2);

Open in new window

0
Comment
Question by:bbdesign
  • 2
  • 2
  • 2
6 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 1400 total points
ID: 36550949
it work of course : http://jsfiddle.net/4pMJR/

if you see a different value, it's because :
- you do'nt use the right variable
or :
- you change it somewhere else (client or server side)
or :
- you display value of the variable with a function doing a bad job
0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 600 total points
ID: 36550957
it should be, since below one is working for me

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
      <script>
            var float1 = 1213212.12312;
            alert(float1);
            alert(float1.toFixed(2));
      </script>
</HEAD>

<BODY>
      
</BODY>
</HTML>


0
 

Author Comment

by:bbdesign
ID: 36551037
My entire function is attached. Here is where weight2b is calculated and then displayed inside a text box:

var weight2b=(5*(parseFloat($('#user_weight').val())/150)*6.33).toFixed(2);
$('#weight2b').val(weight2b);

It is used in calculating weight2d a few lines later, but it shouldn't be modified by that statement (I don't think).

As far as I can tell, I am using the correct variable. This is all happening on the client side, no server side. Is there something about how I am putting the value in the text box that is incorrect?
$('#weight1d').click(function(){
	if ($('#weight1a').val().length>0 && $('#weight1b').val().length>0){
		if (isNumber($('#weight1a').val()) && isNumber($('#weight1b').val())){
			$('#weight1a').removeClass('redbg');
			$('#weight1b').removeClass('redbg');
			var weight2aStartDate=Date.parseExact($('#startdate option:selected').val(), 'M/d/yyyy');
			var weight2aBeginDate=Date.parseExact($('#startdate option:selected').val(), 'M/d/yyyy');
			var weight2aTerm=parseInt($('#term').val());
			var weight2aEndDate=weight2aStartDate.addMonths(weight2aTerm);
			var weight2a=((parseFloat($('#weight1b').val())*3500)/parseInt(days_between(weight2aBeginDate, weight2aEndDate))).toFixed(2);
			$('#weight2a').val(weight2a);
			var weight2b=(5*(parseFloat($('#user_weight').val())/150)*6.33).toFixed(2);
			$('#weight2b').val(weight2b);
			var weight2c=(weight2a/((parseFloat($('#user_weight').val())/150)*6.33)).toFixed(0);
			$('#weight2c').val(weight2c);
			var weight2d=((6.33*weight2c)-weight2b).toFixed(0);
			if (weight2d<0){weight2d=0;}
			$('#weight2d').val(weight2d);
			savedata();
			$('#weightlosscalculator').animate({'backgroundColor':'#ffff66'}, 50);
			$('#weightlosscalculator').delay(200).animate({'backgroundColor':'#ffffff'}, 750);
		} else {
			if (isNumber($('#weight1a').val())==false){$('#weight1a').addClass('redbg');}
			if (isNumber($('#weight1b').val())==false){$('#weight1b').addClass('redbg');}
		}
	} else {
		if ($('#weight1a').val().length==0){$('#weight1a').addClass('redbg');}
		if ($('#weight1b').val().length==0){$('#weight1b').addClass('redbg');}
	}
});

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 83

Expert Comment

by:leakim971
ID: 36551056
replace line 11 by : $('#weight2a').val(weight2a);alert(  $('#weight2a').val() );

what do you see in the alert box?

0
 

Author Comment

by:bbdesign
ID: 36551069
Strangely enough, now it is working. I have no idea what I was doing wrong, or what I did to fix it, but now all my values are coming out right. Thanks for helping!
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36551074
can you divide it into

var userWeight = parseFloat($('#user_weight').val());
alert(userWeight);
var weight2b=(userWeight /150)*6.33*5;
alert(weight2b);
var weight2bFixed = weight2b.toFixed(2);
alert(weight2bFixed);
$('#weight2b').val(weight2b);

let me know what is the alert values
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses
Course of the Month16 days, 14 hours left to enroll

862 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