Sunday, May 5, 2019

How to convert string value (ex: double value) to percentage in javascript?











I would like to know how we can convert the following value to a percentage in javascript?



First Expected Output




 Input Value   :  0.03222772304422584
Expected Value: 0.03


Second Expected Output



 Input Value   :  0.03222772304422584
Expected Value: .03



Thanks in advance,



Kathir


Answer



You can use toFixed() and slice() like:



var n = +"0.0322277230442258"; //converts String to Number
var r = n.toFixed(2); // returns 0.03

var r2 = r.slice(1): // returns .03 by removing the first character


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...