1

Please see below code,i getting wrong value.

eg;

 var FirstValue=0.00;
 var secondvalue=parseFloat("22.88",10).toFixed(2);    
 var thirdvalue=(FirstValue) +   (secondvalue);

am getting value like"22.8822.88"

Please help me to solve.Its not convert to numeric .

2
  • Is this actually your code? I don't get the correct result but I don't get what you're seeing. Also you have an extra closing ) after toFixed. jsfiddle.net/DJSRV/3 Commented May 24, 2012 at 14:12
  • Thanks..its not a actual code..yes there there is extra closing. i removed it. Commented May 24, 2012 at 14:15

2 Answers 2

4

toFixed convert you float value to string back. So when you adding two values you will get not number addition but string concatenation:

2.0 + 2.0 = 4.0 // number
"2.0" + "2.0" = "2.02.0" // string

Remove to fixed after conversion. Than add two values and than do to fixed:

var FirstValue=0.00;
var secondvalue=parseFloat("22.88",10);    
var thirdvalue= ( (FirstValue) +   (secondvalue) ).toFixed(2);
Sign up to request clarification or add additional context in comments.

3 Comments

ok.Its fine for floating .but my secondvalue may be interger like "10",am using this inside loop.
then its giving error like "Microsoft JScript runtime error: Object doesn't support this property or method"
I test it on IE6 and FireFox - it works for me. Copy past this code - and it's work. Maybe there is other problem in your code?
0

try this:

 var FirstValue=0.00;
 var secondvalue=parseFloat("22.88");
 secondvalue = parseFloat(secondvalue.toFixed(2));   
 var thirdvalue=(FirstValue) +   (secondvalue);

toFixed returns a string, not a number so it needs to be converted again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.