469,923 Members | 1,633 Online

Test for decimal length/size

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

Nov 21 '06 #1
3 12162 mohaaron wrote:
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,

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

--
Tom Porterfield

Nov 21 '06 #2
Tom,

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

Regards,

Aaron

On Nov 21, 10:58 am, "Tom Porterfield" <tppor...@mvps.orgwrote:
mohaaron wrote:
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,

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

--
Tom Porterfield
Nov 22 '06 #3
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
Nov 22 '06 #4

 21 posts views Thread by Batista, Facundo | last post: by 17 posts views Thread by John Bentley | last post: by 2 posts views Thread by MAF | last post: by 3 posts views Thread by vp | last post: by 1 post views Thread by ianhen | last post: by 28 posts views Thread by FAQ server | last post: by 8 posts views Thread by sdlt85 | last post: by 2 posts views Thread by emily224 | last post: by 4 posts views Thread by emily224 | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by lanliddd | last post: by reply views Thread by isladogs | last post: by 1 post views Thread by isladogs | last post: by reply views Thread by Trystan | last post: by reply views Thread by Waqarahmed | last post: by 1 post views Thread by skydivetom | last post: by 1 post views Thread by MikeCant | last post: by