Check Decimal Places in C#

G

Guest

Hi all,

I have a textbox to let user to input digits. After they input, I want to count how many decimal places they input. How can I do?

Regards,
 
R

RichB

Not sure if it is the best method, but the following works:

<textboxid>.Text.Length - (<textboxid>.Text.IndexOf(".")+1)

Philip said:
Hi all,

I have a textbox to let user to input digits. After they input, I want to
count how many decimal places they input. How can I do?
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi,

It works... only if there's no empty places in text box,
and it works for CultrureInfo with "." as a number decimal
separator!

Some changes:

string numberStr=<textboxid>.Text.Trim();
string decSeparator=NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
// or "NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator"

int index=numberStr.IndexOf(decSeparator);
int decPlaces;
if( index==-1 ) {
decPlaces=0;
}
else {
decPlaces=numberStr.Length - (index+decSeparator.Length);
}

Cheers!

Marcin
 
G

Guest

Thanks a lot. It works.

Marcin Grzębski said:
Hi,

It works... only if there's no empty places in text box,
and it works for CultrureInfo with "." as a number decimal
separator!

Some changes:

string numberStr=<textboxid>.Text.Trim();
string decSeparator=NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
// or "NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator"

int index=numberStr.IndexOf(decSeparator);
int decPlaces;
if( index==-1 ) {
decPlaces=0;
}
else {
decPlaces=numberStr.Length - (index+decSeparator.Length);
}

Cheers!

Marcin
 

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

Top