How to convert imperial units of length into metric?

前端 未结 5 1750
心在旅途
心在旅途 2021-01-14 07:16

Here I am faced with an issue that I believe(or at least hope) was solved 1 million times already. What I got as the input is a string that represents a length of an object

5条回答
  •  Happy的楠姐
    2021-01-14 08:05

    The imperial string values are a little bit more complicated, so I used following expression:

    string pattern = "(([0-9]+)')*\\s*-*\\s*(([0-9])*\\s*([0-9]/[0-9])*\")*";
    Regex regex = new Regex( pattern );
    Match match = regex.Match(sourceValue);
    if( match.Success ) 
    {
        int feet = 0;
        int.TryParse(match.Groups[2].Value, out feet);
        int inch = 0;
        int.TryParse(match.Groups[4].Value, out inch);
        double fracturalInch = 0.0;
        if (match.Groups[5].Value.Length == 3)
             fracturalInch = (double)(match.Groups[5].Value[0] - '0') / (double)(match.Groups[5].Value[2] - '0');
    
        resultValue = (feet * 12) + inch + fracturalInch;
    

提交回复
热议问题