-1

I am trying to limit the user input a number within 5 decimal points(e.g. 0.00007, 1.23456), if user input more than 5 decimal points, it will show a error message. How can I check it that is it longer than 5 dc? Thanks

public boolean isDc(float userInput)
{
    if(/** if userinput is within 5 dc**/)
         return true;
    else
         MessageBox.show("You should input within 5dc(E.g. 0.00001, 1.12345)");

    return false;
}
Capslock10
  • 796
  • 2
  • 16
  • 37
  • You can convert it to string then split on '.' and check its length. – Jasen Dec 14 '16 at 02:59
  • 4
    A user input is a string. So you test the string and then convert to a float. – jdweng Dec 14 '16 at 02:59
  • 4
    I don't think this is C#... – timleathart Dec 14 '16 at 03:00
  • 1
    Closely related to [this](http://stackoverflow.com/a/9387756/643104) – Mahesh Dec 14 '16 at 03:00
  • 1
    If you are using c# and you care about the precison of the decimal point then I suggest using `decimal` instead of `float`. – juharr Dec 14 '16 at 03:03
  • @timleathart LOL, I forgot `Function` should change `public\private`. – Capslock10 Dec 14 '16 at 03:09
  • If you must use `float` code will be very awkward and result in accepting only small subset of values (as only fraction of `float` numbers can be represented with exactly 5 digits after the decimal point)... If `decimal` is ok than suggested duplicate covers it. – Alexei Levenkov Dec 14 '16 at 03:12
  • @juharr Yes, I changed to decimal now, ty. – Capslock10 Dec 14 '16 at 03:19
  • So "0.12345" is acceptable input, but "0.123450" is excessively precise? If so, you need to inspect the user input, not a converted value as is done in the accepted answer. – HABO Dec 14 '16 at 04:19

2 Answers2

2

This might do the trick for you but the userInput should be decimal

if(BitConverter.GetBytes(decimal.GetBits(userInput)[3])[2] == 5)
{
   //Show the error message
}

This will only check that if you have 5 decimal places or not. You can have a look at Find number of decimal places in decimal value regardless of culture and thus

First way could be

public boolean isDc(decimal userInput)
{
    if(BitConverter.GetBytes(decimal.GetBits(userInput)[3])[2] == 5)
         return true;
    else
         MessageBox.show("You should input within 5dc(E.g. 0.00001, 1.12345)");

    return false;
}

or

public boolean isDc(float userInput)
{
    decimal outdc;
    if(decimal.TryParse(userInput.ToString(), out outdc))
    {
        if(BitConverter.GetBytes(decimal.GetBits(outdc)[3])[2] == 5)
             return true;
        else
             MessageBox.show("You should input within 5dc(E.g. 0.00001, 1.12345)");
    }
    return false;
}
Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69
1
 private Boolean isDc(string userInput)
        {
            var num = Decimal.Parse(userInput); //Use tryParse here for safety
            if (decimal.Round(num, 5) == num)            
                return true;
            else
                MessageBox.Show("You should input within 5dc(E.g. 0.00001, 1.12345)");

            return false;
        }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396