-4

I have done this with string values like 65.34, 65,45, 34.45

Everything returns True ...

public void test()
{
   string value = "65" 0r "65.0" 0r "1,234.54";
   decimal number;

   if (Decimal.TryParse(value, out number))
      MessageBox.Show(value);
   else
      MessageBox.Show("Unable to parse '{0}'.", value);
 }

This all returns its a Decimal ..

         If I type "0.65" .. I need to show error and it i type "65" it has to be exectued.
user1764824
  • 9
  • 1
  • 3
  • 4
    "If I type 65 .. I need to show its an integer... if I type 65.0 then I have to show error" Yet, your question is about parsing decimals... I'm confused. `65` and `65.0` are valid decimal values. – Jim D'Angelo Oct 26 '12 at 04:48
  • sir if i am typing .65 i have to show error. if i am typing 24.23 i dont have to show error.. how can i do that – user1764824 Oct 26 '12 at 04:58
  • You have not asked an answerable question. It was asked of you to clarify your question--which might remove the downvotes. (Edit your question with clarification, don't put it in comments.) – Jim D'Angelo Oct 26 '12 at 04:58
  • It looks like you want to obtain the fractional digits from a floating point number, or at least verify that it isn't a whole number. Looks like a duplicate of: http://stackoverflow.com/questions/1040707/c-sharp-get-digits-from-float-variable – Frazell Thomas Oct 26 '12 at 05:01
  • 1
    And do you want .65 to be invalid? Because it is lower than 1 or because it does not start with a 0? I'd accept .65 as a valid Decimal otherwise. – Chrono Oct 26 '12 at 07:42

1 Answers1

0

You have two options

1.Use Regex

or

2.USe the following method to find the number of decimal places

 static decimal CountDigitsAfterDecimalPoint(decimal decimalNumber)
        {
            int[] bits = Decimal.GetBits(decimalNumber);
            int exponent = bits[3] >> 16;
            int result = exponent;
            long lowDecimal = bits[0] | (bits[1] >> 8);
            while ((lowDecimal % 10) == 0)
            {
                result--;
                lowDecimal /= 10;
            }

            return result;
        }
Community
  • 1
  • 1
Rockstart
  • 2,337
  • 5
  • 30
  • 59