Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to take the length of a string with zero at the end in Javascript?


if the value of the string is as "123.45". The length is coming as 5 which is fine.

But if the value of the string is as "123.40". The length is coming as 4 instead of 5.
Posted
Comments
hypermellow 20-May-15 6:58am    
Can you post your code of how you are checking the length of your strings please?

String "123.45" has a length of 6. If your code is returning a length of 5 then you must be doing something different to what you say you are doing.
Kandiya 20-May-15 7:13am    
//First Case
var num = 123.40;
str = num.toString();

len = str.length;
alert(len);

//Second Case
var num = 123.45;
str = num.toString();

len = str.length;
alert(len);

1 solution

1. Reply to comment so that the user is notified.
2. Click improve question and put your code in the actual question instead of in a comment.
3. You can't do it like that because 123.40 is the same exact value as 123.400 and 123.4000 so you'll have to put it into a string or format it with that many places. For example you can do:

JavaScript
var num = 123.40;
num = num.toFixed(2); // this will make sure it always has 2 decimal places so that ending in 0 is accounted for.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900