Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Any way to use fractional length? #1214

Unanswered
ppereira-serviceonsites asked this question in Q&A
Discussion options

Often times I need to convert decimals to fractional representation to display to/get input from a user... just for length...
eg.
2.25 in = 2 1/4"

14.625 in = 1' 2 5/8"

Is there any way to do this already that I just haven't come across??

You must be logged in to vote

Replies: 4 comments 3 replies

Comment options

You might be able to use this:

var fi = Length.FromFeet(1.1234).FeetInches;
fi.ToArchitecturalString(1); // 1' - 1"
fi.ToArchitecturalString(2); // 1' - 1 1/2"
fi.ToArchitecturalString(5); // 1' - 1 2/5"
You must be logged in to vote
0 replies
Comment options

Hey Andreas... thanks loads for this! From: Andreas Gullberg Larsen ***@***.***> Sent: Friday, February 24, 2023 11:19 AM To: angularsen/UnitsNet ***@***.***> Cc: Pascal Pereira ***@***.***>; Author ***@***.***> Subject: Re: [angularsen/UnitsNet] Any way to use fractional length? (Discussion #1214) You might be able to use this: var fi = Length.FromFeet(1.1234).FeetInches; fi.ToArchitecturalString(1); // 1' - 1" fi.ToArchitecturalString(2); // 1' - 1 1/2" fi.ToArchitecturalString(5); // 1' - 1 2/5" — Reply to this email directly, view it on GitHub<#1214 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AQI5TUOBCEW7JZIGNG2VWIDWZCKLLANCNFSM6AAAAAAVGXTZGY>. You are receiving this because you authored the thread.Message ID: ***@***.******@***.***>>
You must be logged in to vote
0 replies
Comment options

Hey Andreas, This works perfectly... However, I also have a need to go the other way. My customers are taking measurements in imperial... and I need to take that and convert to metric. So... looking at your code... in the TryParseFeetInches function, I notice that we are not accounting for fractions... For example, this works as input: 2’ 4" but this does not: 2’ 4 1/2" Is there some way we could make a fix for this? Thanks, Pascal. From: Andreas Gullberg Larsen ***@***.***> Sent: Friday, February 24, 2023 11:19 AM To: angularsen/UnitsNet ***@***.***> Cc: Pascal Pereira ***@***.***>; Author ***@***.***> Subject: Re: [angularsen/UnitsNet] Any way to use fractional length? (Discussion #1214) You might be able to use this: var fi = Length.FromFeet(1.1234).FeetInches; fi.ToArchitecturalString(1); // 1' - 1" fi.ToArchitecturalString(2); // 1' - 1 1/2" fi.ToArchitecturalString(5); // 1' - 1 2/5" — Reply to this email directly, view it on GitHub<#1214 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AQI5TUOBCEW7JZIGNG2VWIDWZCKLLANCNFSM6AAAAAAVGXTZGY>. You are receiving this because you authored the thread.Message ID: ***@***.******@***.***>>
You must be logged in to vote
0 replies
Comment options

Hey Andreas, A bit of a follow up on this... I’ve cobbled together this code that works... Its loosely modeled after your TryParseFeetInches function. It allows for the following variations (also... the inches sign is optional on all and negatives are also optional): * 1’ 2 3/4" * 1’-2 3/4" * 1’2 3/4" * 2 3/4" * 2" * 3/4" While this works for now... another thought I had was Length might get another variant... you have ft/in/etc... perhaps it might be a good idea to have "Architectural" unit. On the fence on that but throwing it out there. Hope this helps... the regex string alone is a good few hours of work. Thanks, Pascal. public bool TryParseArchitecturalString(string? str, out Length result, IFormatProvider? formatProvider = null) { if (str == null) { result = default; return false; } str = str.Trim(); // Match entire string exactly string pattern = @"^(?<negativeSign>\-?)[ ]?((?<feet>\d+)')?(?:(?<=')\s*[-]?\s*)?(((((?<inches>\d+))[ ]((?<numerator>\d+)\/(?<denominator>\d+|\.\d+))+)|(((?<numerator>\d+)\/(?<denominator>\d+|\.\d+))+)|((?<inches>\d+)))""?)?$"; var match = new Regex(pattern, RegexOptions.Singleline).Match(str); if (!match.Success) { result = default; return false; } var negativeSignGroup = match.Groups["negativeSign"]; var feetGroup = match.Groups["feet"]; var inchesGroup = match.Groups["inches"]; var numeratorGroup = match.Groups["numerator"]; var denominatorGroup = match.Groups["denominator"]; Length feet = new Length(0, LengthUnit.Foot); Length inches = new Length(0, LengthUnit.Inch); Length fraction = new Length(0, LengthUnit.Inch); int numGroupsSatisfied = 0; if (feetGroup.Length > 0) { double dblRet; if (Double.TryParse(feetGroup.Value, out dblRet)) { feet = new Length(dblRet,LengthUnit.Foot); numGroupsSatisfied++; } } if (inchesGroup.Length > 0) { double dblRet; if (Double.TryParse(inchesGroup.Value, out dblRet)) { inches = new Length(dblRet, LengthUnit.Inch); numGroupsSatisfied++; } } if (numeratorGroup.Length > 0 && denominatorGroup.Length>0) { double dblDenominator; double dblNumerator; if (Double.TryParse(denominatorGroup.Value, out dblDenominator) && Double.TryParse(numeratorGroup.Value,out dblNumerator)) { // check for divide by 0 if (dblDenominator == 0) { result = default; return false; } double dblFraction = dblNumerator / dblDenominator; fraction = new Length(dblFraction, LengthUnit.Inch); numGroupsSatisfied++; } } if (numGroupsSatisfied>0) { result = feet + inches + fraction; if (negativeSignGroup.Length > 0) result = -result; return true; } result = default; return false; } From: Pascal Pereira Sent: Friday, March 10, 2023 9:25 AM To: angularsen/UnitsNet ***@***.***>; angularsen/UnitsNet ***@***.***> Cc: Author ***@***.***> Subject: RE: [angularsen/UnitsNet] Any way to use fractional length? (Discussion #1214) Hey Andreas, This works perfectly... However, I also have a need to go the other way. My customers are taking measurements in imperial... and I need to take that and convert to metric. So... looking at your code... in the TryParseFeetInches function, I notice that we are not accounting for fractions... For example, this works as input: 2’ 4" but this does not: 2’ 4 1/2" Is there some way we could make a fix for this? Thanks, Pascal. From: Andreas Gullberg Larsen ***@***.******@***.***>> Sent: Friday, February 24, 2023 11:19 AM To: angularsen/UnitsNet ***@***.******@***.***>> Cc: Pascal Pereira ***@***.******@***.***>>; Author ***@***.******@***.***>> Subject: Re: [angularsen/UnitsNet] Any way to use fractional length? (Discussion #1214) You might be able to use this: var fi = Length.FromFeet(1.1234).FeetInches; fi.ToArchitecturalString(1); // 1' - 1" fi.ToArchitecturalString(2); // 1' - 1 1/2" fi.ToArchitecturalString(5); // 1' - 1 2/5" — Reply to this email directly, view it on GitHub<#1214 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AQI5TUOBCEW7JZIGNG2VWIDWZCKLLANCNFSM6AAAAAAVGXTZGY>. You are receiving this because you authored the thread.Message ID: ***@***.******@***.***>>
You must be logged in to vote
3 replies
Comment options

Thanks, I'll take a closer look soon

Comment options

I think this looks useful, can you create a pull request and write some unit tests that cover the new inputs?

  1. We need to support all registered unit abbreviations for a given culture, such as ft and '. See how it's done in TryParseFeetInches.
  2. An alternative approach could be to replace fractionals with leading whitespace, such as 1/4 => .25 so that 1' 1/4" becomes 1' .25" and 1' 2 1/4" becomes 1' 2.25". Maybe there are pitfalls to that approach, but it might allow us to reuse the existing regex.
Comment options

I would also suggest to improve the existing TryParseFeetInches() method instead of creating a new method, since it seems useful to those parsing feet and inches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /