rounding number to two decimal places not working

In the attached code, based on values on my screen, the variable is spitting out:


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

Brad BansnerWeb DeveloperAsked:
Who is Participating?
leakim971Connect With a Mentor PluritechnicianCommented:
it work of course :

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
Gurvinder Pal SinghConnect With a Mentor Commented:
it should be, since below one is working for me

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
            var float1 = 1213212.12312;


Brad BansnerWeb DeveloperAuthor Commented:
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);

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?
	if ($('#weight1a').val().length>0 && $('#weight1b').val().length>0){
		if (isNumber($('#weight1a').val()) && isNumber($('#weight1b').val())){
			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);
			var weight2b=(5*(parseFloat($('#user_weight').val())/150)*6.33).toFixed(2);
			var weight2c=(weight2a/((parseFloat($('#user_weight').val())/150)*6.33)).toFixed(0);
			var weight2d=((6.33*weight2c)-weight2b).toFixed(0);
			if (weight2d<0){weight2d=0;}
			$('#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

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

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

what do you see in the alert box?

Brad BansnerWeb DeveloperAuthor Commented:
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!
Gurvinder Pal SinghCommented:
can you divide it into

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

let me know what is the alert values
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.