I need to do some calculations with fractions which result have infinite decimals.
For example:
240/360=0.666666...
The output has infinite decimals and when I multiply this by an integer the result must be an integer. So I’ve coded this way:
result = someInteger * decimal.divide(240/360);
(Try with someInteger = 2,700)
But the result has decimal values and some calculators or even spreadsheets would output an integer result.
How do I get same results in c#?
Thanks
That won’t really happen.
What you can do is careful rounding (after the multiplication).
Or instead of
you can use
what’s the problem with that?
When you’re really serious about working with fractions and keeping the precision you’ll need a special type:
There are libraries and examples for how to do this.
But note that you still won’t be able to represent π exactly.