0

Here is the scenario I am getting a value from user which I have to parse to float by rounding it up to a certain decimal point but user value can contain "," I need to put , at same place where it was in user value for example if user entered +55,658,698.2396 and value is to be parsed to 2 decimal points the output should be +55,658,698.24 but after parsing I get +55658698.24 Here is the sample code

var userInput = "+54,568,568.14589";
var decimalPoints = 3;
var convertedValue;

var indexP = userInput.indexOf("+");
//getting rid of commas
var value = userInput.replace(/,/g , "");

value =parseFloat(value).toFixed(decimalPoints);
convertedValue = value.toString(); 
if(indexP == 0){
console.log('Converted Value: +'+convertedValue);
}
else{
console.log('Converted Value: '+convertedValue);
}
console.log('Actual Value: '+userInput);

and Here is the Fiddle http://jsfiddle.net/k5jhha4r/

AddyProg
  • 2,960
  • 13
  • 59
  • 110
  • http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – Donal Oct 02 '14 at 11:21
  • http://stackoverflow.com/questions/4951738/culture-sensitive-parsefloat-function-in-javascript – webduvet Oct 02 '14 at 11:22

0 Answers0