Test for decimal length/size

  • Thread starter Thread starter mohaaron
  • Start date Start date
M

mohaaron

Hello All,

Does anyone know of a way to test a decimal for the number of places it
has before and after the decimal?

I need to do something like this.

decimal value1 = 172.45;
// If value1 has more then two places before the decimal return zero.
// Or if value1 has more then two places after the decimal return zero.

The reason that I need to do this is that I have a calculation being
done and the result comes out to be something like value1. The value
then is saved to a database column that is a decimal (4,2) which then
causes an error at the database level. I would like to test to decimal
value before this happens.

Thanks,

Aaron
 
mohaaron said:
Does anyone know of a way to test a decimal for the number of places it
has before and after the decimal?

I need to do something like this.

decimal value1 = 172.45;
// If value1 has more then two places before the decimal return zero.
// Or if value1 has more then two places after the decimal return zero.

The reason that I need to do this is that I have a calculation being
done and the result comes out to be something like value1. The value
then is saved to a database column that is a decimal (4,2) which then
causes an error at the database level. I would like to test to decimal
value before this happens.

String manipulation is usually the best way to do this. A quick example,
add robustness at your leisure:

decimal value1 = 172.45m;
string [] parts = value1.ToString().Split('.');
if (parts[0].Length > 2 || parts[0].Length > 2)
return 0;
 
Tom,

Thank you. This gets me going in the right direction.

Regards,

Aaron

mohaaron said:
Does anyone know of a way to test a decimal for the number of places it
has before and after the decimal?
I need to do something like this.
decimal value1 = 172.45;
// If value1 has more then two places before the decimal return zero.
// Or if value1 has more then two places after the decimal return zero.
The reason that I need to do this is that I have a calculation being
done and the result comes out to be something like value1. The value
then is saved to a database column that is a decimal (4,2) which then
causes an error at the database level. I would like to test to decimal
value before this happens.String manipulation is usually the best way to do this. A quick example,
add robustness at your leisure:

decimal value1 = 172.45m;
string [] parts = value1.ToString().Split('.');
if (parts[0].Length > 2 || parts[0].Length > 2)
return 0;
 
Hello All,
Does anyone know of a way to test a decimal for the number of places it
has before and after the decimal?

I need to do something like this.

decimal value1 = 172.45;
// If value1 has more then two places before the decimal return zero.
// Or if value1 has more then two places after the decimal return zero.

The reason that I need to do this is that I have a calculation being
done and the result comes out to be something like value1. The value
then is saved to a database column that is a decimal (4,2) which then
causes an error at the database level. I would like to test to decimal
value before this happens.

Thanks,

Aaron

Why not use
value1 = Math.Round(value1, 2);
to round the value to two decimal places?

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top