Determine which function to call based on length o

2019-07-15 19:41发布

In C#, I have a situation where I have two possible numbers in a textbox control.

The numbers can be either:

a) .xxxx

or

b) .xx

How do I write a condition that says, "If the textbox has 4 decimal places, then call this function, otherwise, if the textbox has 2 decimal places, then call this function."

Seems easy, but I don't know how to evaluate the decimal places.

Thanks much!

5条回答
啃猪蹄的小仙女
2楼-- · 2019-07-15 20:17

Condition will evaluate true on two but not four decimal places:

Math.Round( 100*x ) == 100*x

EDIT: above condition works only for Decimal type. Well, following works for real numbers of all types:

( Math.Ceiling( 100 * x ) - 100 * x ) < 10e-8 )

EDIT: Well, if you are interested in strings then use following (extension string contains last point and subsequent digits/numbers):

System.IO.Path.GetExtension( input ).Length
查看更多
手持菜刀,她持情操
3楼-- · 2019-07-15 20:21

You could take advantage of a very obscure feature of the Decimal type. Its internal representation is a 96-bit number with an exponent. The exponent is equal to the number of digits in the fraction, even if the fractional digits are zero. Thus:

public static int GetFractionalDigits(string txt) {
  decimal value = decimal.Parse(txt);
  return (decimal.GetBits(value)[3] >> 16) & 0x7fff;
}

Use decimal.TryParse() if you need to validate the user input.

查看更多
ら.Afraid
4楼-- · 2019-07-15 20:22

You can use Regex

new Regex(@"\.\d{2}$").IsMatch(input.Value)
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-07-15 20:35
if(txtNumber.Text.Split(new[]{'.'})[1].Length == 2)
{
    //case b
}
else
{
    //case a
}

you may want to take the decimal separator from the system's current culture instead of hardcoding the dot.

查看更多
Bombasti
6楼-- · 2019-07-15 20:42

This may not work perfectly depending on your needs, but it works for my purposes and could be useful to someone else.

static class MathCustom
{
    static public byte NumberOfDecimals(decimal value)
    {
        sbyte places = -1;
        decimal testValue;

        do
        {
            places++;
            testValue = Math.Round(value, places);
        } while (testValue != value);

        return (byte)places;
    }

    static public byte NumberOfDecimals(float value)
    {
        sbyte places = -1;
        float testValue;

        do
        {
            places++;
            testValue = (float)Math.Round((decimal)value, places);
        } while (testValue != value);

        return (byte)places;
    }

    /// <summary>
    /// This version of NumberOfDecimals allows you to provide a Maximum
    /// for allowable decimals. This method will allow for the correction
    /// of floating point errors when it is less than 10 or passed in as null.
    /// </summary>
    /// <param name="value">Value to check the number of held decimal places</param>
    /// <param name="knownMaximum"></param>
    /// <returns>The number of decimal places in Value.</returns>
    static public byte NumberOfDecimals(decimal value, byte? knownMaximum)
    {
        byte maximum;
        decimal localValue;
        sbyte places = -1;
        decimal testValue;

        if (knownMaximum == null)
        {
            maximum = 9;
        }
        else
        {
            maximum = (byte)knownMaximum;
        }

        localValue = Math.Round(value, maximum);

        do
        {
            places++;
            testValue = Math.Round(localValue, places);
        } while (testValue != localValue);

        return (byte)places;
    }

}
查看更多
登录 后发表回答