3

I am trying to round a decimal "94.545". I am getting a different result in C#.Net and Javascript. The following code i am using.

C#:

string.Format("{0:0.0}", Convert.ToDouble("94.545")); --> Result 94.6
string.Format("{0:0.0}", Convert.ToDouble("90.769")); --> Result 90.8

Javascript:

parseFloat('94.545').toFixed(1); --> Result 94.5
parseFloat('90.769').toFixed(1); --> Result 90.8

The first value '94.545' is creating issue. I am getting different results between c# and javascript.

I have tried different things, i am getting the same response.

RobinHood
  • 2,367
  • 11
  • 46
  • 73
  • What do you expect as the correct answer? – Sayse Nov 09 '17 at 08:45
  • 1
    what value you expect is it `floor` or `ceiling ` value? both `javascript` and `c#` has a function for it `Math.Ceiling(value), Math.Floor(value)` use this it would help you i hope – Curiousdev Nov 09 '17 at 08:45
  • Just tried it in Linqpad and got 94.5 The conversion is affected by the current thread culture. What is yours set to? – Rob Anthony Nov 09 '17 at 08:46
  • I am fine with 94.6 or 94.5 (single decimal needed) but both js and c# should return the same. – RobinHood Nov 09 '17 at 08:52
  • 1
    It is known that `toFixed()` yields inconsistent rounding for long time. You can try workaround in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round#Decimal_rounding. – Tetsuya Yamamoto Nov 09 '17 at 09:59
  • Possible duplicate of [Reliable JS rounding numbers with toFixed(2) of a 3 decimal number](https://stackoverflow.com/questions/42109818/reliable-js-rounding-numbers-with-tofixed2-of-a-3-decimal-number) – Sandip Bantawa Nov 09 '17 at 10:30
  • Math.Floor(value) gave some idea...Thanks a lot. – RobinHood Nov 10 '17 at 07:18

0 Answers0