I have 2 decimal numbers:
1999,9999
1999,99
if I use function
decimal.Round(Temp, 2);
then I have these results:
2000,00
1999,99
How to make sure that even if there is 1999,999999 it will round to 1999,99 instead of 2000,00.
Thanks.
I have 2 decimal numbers:
1999,9999
1999,99
if I use function
decimal.Round(Temp, 2);
then I have these results:
2000,00
1999,99
How to make sure that even if there is 1999,999999 it will round to 1999,99 instead of 2000,00.
Thanks.
Rounding will always make 1999,999 move to 2000. It sounds like you want to truncate.
You can do this via some multiplication/division:
decimal TruncateToPlaces(decimal value, int places)
{
decimal multiplier = Math.Pow(10m, places);
return decimal.Truncate(value * multiplier) / multiplier;
}
You can then do:
decimal value = TruncateToPlaces(1999.9999, 2);
You need to truncate digits. One way to do it is: decimal.Truncate(<number> * 100) / 100;
Try this:
Convert.ToDecimal(x.ToString("#.##"));
decimal d = 1999.999999; //or any
int temp = d*100; //199999
decimal result = temp/(decimal)100; //199.99
Well the short answer is that....1999,9999
rounded to 2 decimal places is in fact 2000,00
.
There are a number of different rounding strategies — http://en.wikipedia.org/wiki/Rounding — you might want to read up on them.
But if you don't want to round the value and instead want to truncate it, then this should do you. scale
is a power of 10 indicating the position at which truncation should occur. If scale
is a negative power of 10 (e.g., 10-2), then truncation will occur that many digits to the right of the decimal point. If scale
is non-negative, then truncation occurs to the left of the decimal point (e.g., a scale of 1
aka 100 will truncate the fraction, and a scale
of 100 (or 102) will truncate everything to the right of the 100s place, converting 1999.9999m
into 1900m
.
decimal TruncateToHundredthsPlace( decimal value )
{
const decimal scale = 0.01m ; // 10^-2
decimal result = value - ( value % 0.01m ) ;
return results ;
}
Alternatively, you could write a method like the following code. It works for any scale: negative values for scale truncate that many positions right of the decimal point; non-zero values truncate left of the decimal point.
public static decimal Truncate( decimal value , int scale )
{
Decimal factor = Power(10,scale) ;
decimal result = value - ( value % factor ) ;
return result ;
}
private static decimal Power( int m , int n )
{
if ( m < 1 ) throw new ArgumentOutOfRangeException("m") ;
Decimal @base = (decimal) m ;
Decimal factor = 1m ; // m^0 = 1
while ( n > 0 )
{
factor *= @base ;
--n ;
}
while ( n < 0 )
{
factor /= @base ;
++n ;
}
return factor ;
}
Computing a power of 10 in decimal is expensive. Two simple optimization would speed it up, though:
First, precompute some reasonable number of powers of 10 in a static lookup table, say 10-10–10+10 inclusive. I'd create a decimal[]
with a negative lower bound: then the check to see if the specified scale is in the lookup table just consists of checking to see if the scale value is a valid index into the array. If so, pull the factor out using the scale value as the index.
A second optimization would be to cache power of 10 values as you compute them. If you've already computed the value, you don't really need to recompute it, just fish it from cache using the scale as the key.