Format question

  • Thread starter Thread starter OC
  • Start date Start date
O

OC

What would be the best way to check for a decimal number coming in as, for
example, 99999.9999 and truncating it to 99999.99?

IOW, if it is 4 digits after the decimal, truncate it to 2.
 
OC said:
What would be the best way to check for a decimal number coming in as, for
example, 99999.9999 and truncating it to 99999.99?

IOW, if it is 4 digits after the decimal, truncate it to 2.

You could probably fiddle around with string formatting, but for most
decimal numbers, this should work fine:

decimal dec = 99999.9999M;
dec = Decimal.Truncate(dec * 100) / 100;
MessageBox.Show(dec.ToString());

Erik
 
Hello

decimal d = 99999.9999M;
string s = d.ToString("0.00");

but this would format 1.1 as 1.10 and 4 as 4.00

Best regards
Sherif
 
Sherif ElMetainy said:
Hello

decimal d = 99999.9999M;
string s = d.ToString("0.00");

but this would format 1.1 as 1.10 and 4 as 4.00

Best regards
Sherif

That doesn't truncate - it rounds. The result in string s is
"100000.00", not "99999.99".

Erik
 
You could use regular expressions to analyze and get groups of digits before and after the "." sign and then appropriately truncate your decimal part.
 
Back
Top