-4

So , i have a problem . i am calculating the discount and it may be in decimal points . the method i am using is not working for the value 0.0038575 and the method is

value=0.0038575
Math.round(value * 100) / 100,

it is returning the same value . this method is working for all other values . but the expected result is 0.004

now i changed the method i used this one var discount = 0.0038575.toFixed(3); working fine but the issue with this is it is not working if i pass the variable like below

var value=0.0038575;
var discount = value.toFixed(3);

error is value.toFixed is not a functioni also tried this one for solve

var value=0.0038575;
var discount = "'"+value+"'".toFixed(3);

any help will be appreciated . thanx in advance

Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
  • 1
    http://jsfiddle.net/q4zyb17u/ – Naeem Shaikh Feb 20 '15 at 12:11
  • this could help http://stackoverflow.com/questions/2861055/using-tofixed2-and-math-round-to-get-correct-rounding – BeNdErR Feb 20 '15 at 12:12
  • 1
    possible duplicate of [Javascript: formatting a rounded number to N decimals](http://stackoverflow.com/questions/2221167/javascript-formatting-a-rounded-number-to-n-decimals) – collapsar Feb 20 '15 at 12:12
  • MDN's suggestion for a [`round10` decimal rounding function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round) might be of interest. – collapsar Feb 20 '15 at 12:15

2 Answers2

0

Following snippet works for me:

var value = 0.0038575;
var discount = value.toFixed(3);
console.log(discount);

If your value is of type string you can try to cast it to a number with the + operator:

var discount = (+value).toFixed(3);

But without any reproducible code snippet I can only guess an answer.

cmxl
  • 663
  • 12
  • 24
0

this worked for me

var value = 0.0038575;
var loss = parseFloat(value).toFixed(3);
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68