Friday 15 July 2011

c# - divide the money value to zero? -



c# - divide the money value to zero? -

i have problem split money value "0". build accounting software, shrinking asset.

example, have value 1000000. split 3.

1000000 / 3 = 333333,33333333333333333333333

the question is, this:

step 1 :1000000 - 333333,33333333333333333333333 = 666666,66666666666666666666667

step 2 :666666,66666666666666666666667 - 333333,33333333333333333333333 = 333333,33333333333333333333334

step 3 :333333,33333333333333333333334 - 333333,33333333333333333333333 = 0,00000000000000000000001

example code

decimal value = 100; int divider = 3; decimal x = value / divider; (int = 0; < divider; i++) { value = value - x; }

the result, value = 0,000000000000000000000000001 .

yeah, problem. money value, split zero.

i've tried using math method "math.floor" @ each step (on loop). result value -2. know can using "math.floor" method final result. don't, because want step step (on loop.) should do?

thanks

you should consider multiplying 1/3 1, 2, or 3 instead of subtracting previous number. should work:

var originalamount = 1000000d; var thirdoforiginal = originalamount / 3; console.writeline("original number ... {0}", originalamount); console.writeline("original - 1/3 .... {0}", (originalamount - (thirdoforiginal * 1))); console.writeline("original - 2/3 .... {0}", (originalamount - (thirdoforiginal * 2))); console.writeline("original - 3/3 .... {0}", (originalamount - (thirdoforiginal * 3))); // output // original number ... 1000000 // original - 1/3 .... 666666.666666667 // original - 2/3 .... 333333.333333333 // original - 3/3 .... 0

this written more generically as:

private static list<decimal> getamounts(decimal originalnumber, int numberofdivisions) { var amounts = new list<decimal>(); if (numberofdivisions > 0) { var fraction = originalnumber / numberofdivisions; (int = 1; <= numberofdivisions; i++) { amounts.add(originalnumber - (fraction * i)); } } homecoming amounts; }

c# sql-server accounting

No comments:

Post a Comment