Any way to use fractional length? #1214
Unanswered
ppereira-serviceonsites
asked this question in
Q&A
Replies: 4 comments 3 replies
-
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" |
Beta Was this translation helpful? Give feedback.
0 replies
-
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: ***@***.******@***.***>>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
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: ***@***.******@***.***>>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
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: ***@***.******@***.***>>
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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??
Beta Was this translation helpful? Give feedback.
All reactions