Sunday 5 May 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...