How do you count the number of decimals in a decimal datatype
-
I have program that will input an amount of money so I do not want the user to input something like $2.684. I need to be able to count the number of decimals so I can create an if statement that prevents the user from inputting such a number.
-
I have program that will input an amount of money so I do not want the user to input something like $2.684. I need to be able to count the number of decimals so I can create an if statement that prevents the user from inputting such a number.
Yes. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
I have program that will input an amount of money so I do not want the user to input something like $2.684. I need to be able to count the number of decimals so I can create an if statement that prevents the user from inputting such a number.
subtract the integer part multiply by 100 subtract the integer part, again if result not equal to 0 then you've got a problem.
-
I have program that will input an amount of money so I do not want the user to input something like $2.684. I need to be able to count the number of decimals so I can create an if statement that prevents the user from inputting such a number.
well if you are using a textbox then you will need a number of different validations... 1. Check the currency symbol exists only once and is the first character, if not present you should auto add it 2. Check there is only 1 decimal point 3. Check for any , symbols you may want to include and insure they are in the right places 4. Check only two digits after the decimal place... if(text.LastIndexOf('.') != text.Length - 3) //not two digits after the decimal point ...there are other validations you may want to try and other methods to calculate the number of digits after a decimal. you could also use a NumericUpDown control which has a property to set how many decimal places you wish to use, in addition to choosing if you want to you commas for thousand seperators
Life goes very fast. Tomorrow, today is already yesterday.