[Mono-list] Bug in the scale of the result of decimal division

Alex Peterson peterdj at telkomsa.net
Fri Feb 10 10:17:47 UTC 2012


I've had a similar issue. A rather agly fix is to convert to string and back
as shown below, or use Decimal.Round() or Decimal.Truncate().

string str;
if (data == decimal.Zero)
	str = decimal.Zero.ToString();
else {
	str = data.ToString();
	bool hasDecimalPoint = false;
	foreach (char c in str)
		if (c == '.') {
			hasDecimalPoint = true;
			break;
		}

	// remove trailing zeros
	if (hasDecimalPoint) {
		int truncIx = str.Length - 1;
		while (truncIx > 1 && str[truncIx] == '0')
			--truncIx;
		if (str[truncIx] == '.')
			--truncIx;
		if (truncIx < str.Length - 1)
			str = str.Substring(0, truncIx + 1);
	}
}

--
View this message in context: http://mono.1490590.n4.nabble.com/Bug-in-the-scale-of-the-result-of-decimal-division-tp4375794p4375815.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list