-4

Test cases:

var num1 = 10.66;
var num2 = 10.7898

The function I found on stackOverFlow:

function formatUserCurrencyValue(fieldValue){
    var num = parseFloat(fieldValue);
    var str = num.toFixed(10);
    str = str.substring(0, str.length-7);
    return str;
};

I would like the result to be like this: if 10.66 then 10.670 and if 10.78998 then 10.789. Basically if the value has 2 decimal places then the result should round up the first and then format as 3 decimals. If more than 2 decimals (eg. 10.78998) then 10.789, trimming out values after 3 decimals.

Could somebody please tell me how I can achieve this? I tried with the above function as well as some others I found but the result is not what I expected for the 10.66 scenario. I am getting 10.660 instead of 10.670.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
user007
  • 9
  • 3
  • 6
    how does 10.66 become 10.67? – depperm Jul 13 '15 at 19:47
  • Surely `10.660` is the correct answer given mathematical laws? Is `10.670` just a typo in your question? – Rory McCrossan Jul 13 '15 at 19:53
  • I am also confused on your rounding... 10.78998 should be rounded to 10.790 according to the rules of rounding... Can you explain your specific rules a little clearer? – Brian Gerhards Jul 13 '15 at 19:54
  • no for 10.78998 my requirement says no rounding just the three decimal and strip out the remaining values. for 10.66, it just have to be 10.670. – user007 Jul 13 '15 at 20:33
  • 10.66 becomes 10.67 is not logical and doesn't conform to normal rounding as mentioned. Unclear what rules are – charlietfl Jul 13 '15 at 22:13

3 Answers3

0

It looks like a similar question has already been asked here: Formatting a number with exactly two decimals in JavaScript

I liked the answer that @Miguel had. Using a function to do the conversion.

 function precise_round(num, decimals) {
var t=Math.pow(10, decimals);   
 return (Math.round((num * t) + (decimals>0?1:0)*(Math.sign(num) * (10 / Math.pow(100, decimals)))) / t).toFixed(decimals);
    }

Then use the function. precise_round(num,3)

Community
  • 1
  • 1
Klarence
  • 105
  • 2
  • 5
0

Setting aside the fact that rounding num1 will produce 10.66 and not 10.67 what you want can be achieved with the below function which will print the appropriate results to the console.

var num1 = 10.66;
var num2 = 10.7898;
var roundIt = function(num) {
    console.log(parseFloat(Math.round(num * 1000) / 1000).toFixed(3));
};
roundIt(num2); //10.790
roundIt(num1); //10.660

Since using the toFixed() method returns a string we wrap the result in parseFloat() so that we get a floating point number.

HelloWorld
  • 2,480
  • 3
  • 28
  • 45
0

Here you have what you ask, it seems weird to me. Round all like this is something "special" at least... but, is exactly what you ask.

var weirdRound = function(n) {
  var splited = n.toString().split(".");
  var res = 0;
  if(splited[1]) {
    var len = splited[1].length;
    if(len > 3) {
      splited[1] = splited[1].substr(0, 3);
      res = (splited.join(".") *1).toFixed(3);
    } else if(len == 2) {
      splited[1] = splited[1].substr(0, 1) + ((splited[1].substr(len -1, len) *1) +1);
      res = (splited.join(".") *1).toFixed(3);
    } else if(len == 1) {
      res = n.toFixed(2)
    } else {
      res = n.toFixed(3);
    }
  }
  return res.toFixed(3);
}

console.log(weirdRound(10.66));
console.log(weirdRound(10.9));
console.log(weirdRound(10.7898));
console.log(weirdRound(1.12));
console.log(weirdRound(1.12565));
console.log(weirdRound(1.125));
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Lucas Tettamanti
  • 1,360
  • 8
  • 10
  • when you have only one decimal... what do you want? I mean, if you have 1.9 what do you want 2.000 as result? I didn't know what to do in that kind of cases... its obvious for me you aren't follow regular maths... so, you can take this code and modify to your needs. Hope it helps. – Lucas Tettamanti Jul 13 '15 at 21:13
  • 1
    it should be 1.90. i know what i am asking here is stupid but this is what my requirement says and i spent hours on achieving this but no luck. – user007 Jul 14 '15 at 20:20
  • ok, take my code almost exactly (you only need to add the one decimal case toFixed(2) and thats it.. is what you are looking for. I cannot understand why people downvote you.. we are coders, people ask us things and we make those things happens. We can make questions but sometimes we must do what other people wants. – Lucas Tettamanti Jul 14 '15 at 20:25
  • Thanks for your help. – user007 Jul 15 '15 at 20:57