Reputation: 2691
I have in database the field called Profit. In my application I calculating profit:
myFunds.Profit = float.Parse(Math.Round(myFunds.Profit.Value * myFunds.Stake / myFunds.Stake, 3, MidpointRounding.AwayFromZero).ToString());
Type of myFunds.Profit is int?. Math.Round return value as double and is casting to float. In the database profit is saved as 5,40000009536743. I know that float has less precision than double. It is possibility to cut numbers after seventh decimal place ?
Thanks
Upvotes: 0
Views: 2012
Reputation: 1502116
You shouldn't be using binary floating point for currency, basically. Use something which preserves decimal digits instead of a binary representation. It's not a matter of there being "garbage after the seventh digit" - it's a matter of using the wrong representation to start with.
See my articles on binary floating point and decimal floating point for more information.
It's really important that you understand the differences, and why it's simply not acceptable to use binary floating point for monetary values with precise values which cannot be represented in binary floating point. Don't try to fudge around the problems you're having with float
and double
- move to the appropriate data type instead. It may be more work in the short term, but it will be vastly preferable in the longer term.
Upvotes: 14