Skip to content

Instantly share code, notes, and snippets.

@craibuc
Created December 5, 2013 22:50
Show Gist options
  • Select an option

  • Save craibuc/7815455 to your computer and use it in GitHub Desktop.

Select an option

Save craibuc/7815455 to your computer and use it in GitHub Desktop.
Converts a fractional value in the format of Numerator/Denominator or Whole + Numerator/Denominator to a decimal value.
//Assumes values in the following formats:
//99 - integer value
//99.9 - decimal value
//N/D - numerator/denominator
//M N/D - whole + numerator/denominator
Function (Stringvar value)
Local Numbervar whole := 0;
Local Stringvar fraction;
//
value := trim(value);
//integer or decimal format
If IsNumeric(value) Then
ToNumber(value)
Else (
//whole + numerator/denominator
If InStr(value, " ")>0 Then (
whole := ToNumber(Split(value, " ")[1]);
fraction := Split(value, " ")[2];
)
//numerator/denominator
Else (
fraction := value;
);
Local Numbervar numerator := ToNumber(Split(fraction, "/")[1]);
Local Numbervar denominator := ToNumber(Split(fraction, "/")[2]);
whole + (numerator / denominator);
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment