AnsweredAssumed Answered

Convert fraction to decimal number issue

Question asked by sk on Apr 30, 2015
Latest reply on Apr 30, 2015 by sk

Title

Convert fraction to decimal number issue

Post

Hi,

I need help on modifying this great custom function (see below) found on Brian Dunning's website.

I want to be able to make it work when a fraction is entered as 1-1/2 to give me a decimal number of 1.5. Right now with the custom function it gives me a value of 5.5. The "-" is culprit that causes the issue but I can't figure out how to modify it. I tried a substitute but I still can't get it to work.

ConvertFractToNum (number)

Let ( [
$number = Trim(number);
$integer = LeftWords($number;1);
$denominator = Right ( $number ; Length($number)-Position($number; "/";1;1 ));
$numerator = Middle ( $number ; Position ( $number ; " "; 1 ; 1 ) ; Position ( $number ; "/"; 1 ; 1 )-Position ( $number ; " "; 1 ; 1 ) )
];

Case(
// no slash, so no fraction
PatternCount($number;"/")=0;$number;

// no space, so no valid fraction
PatternCount($number;" ")=0;$numerator/$denominator;

// too many spaces, so no valid fraction
PatternCount($number;" ")>1;$number;

// - we have a fraction to parse
$integer + ($numerator/$denominator)
)
)

Sam Barnum, 360Works
November 16, 2010 11:48am

Outcomes