开发者

How to round currency (decimals) to Millions? (C#)

开发者 https://www.devze.com 2023-04-03 06:32 出处:网络
I have decimal currency amounts (from SQl2k* tables as 32,8) and I would like to round them to Millions w开发者_C百科ith 2 decimal places.

I have decimal currency amounts (from SQl2k* tables as 32,8) and I would like to round them to Millions w开发者_C百科ith 2 decimal places.

The system will be displaying 1000s of rows with multiple amounts in them, and I need to summarise the totals for various categories into a "quick view".

e.g.

123,456,789.12345678
goes to
123.46

6,655,443,332.2110099
goes to
6,655.44

etc

I know there are issues with rounding and decimal/floating point math:

45,454,454.454545
goes to
45.46 OR 45.45 ?

so also any advice on what's best to use would also be very much appreciated.


Two more examples (of what I'd EXPECT...not what might actually be mathematically correct!)

555444444.444444 => 555.44
555444444.444445 => 555.45

I would expect the final "5" of the second example to cascade up to the decimal place?


decimal d = Math.Round(inValue/1000000m, 2);


 double v = Math.Round(45454454.454545, 2);

Will return 45454454.45

So solution would be:

 double factor = 1000000;
 double v1 = Math.Round(123456789.12345678 / factor, 2); //// 123.46  
 double v2 = Math.Round(6655443332.2110099 / factor, 2); //// to 6,655.44 


IMHO You should use Bankers Rounding instead of Your 'cascade' rounding

0

精彩评论

暂无评论...
验证码 换一张
取 消