KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Fraction strings using UNICODE
PRODUCT: 4D | VERSION: 13.2 | PLATFORM: Mac & Win
Published On: December 20, 2012

Wouldn't it be nice to be able to represent fractions using proper superscript and subscript and 40 texts instead of using the words such as "thirteen-sixteenths" or 13/16? With the proper use of Unicode that is completely possible. As shown in the graphic below using the code method provided one can easily build Unicode representations of fractions of any type. With this method there is no limit to what fraction can be built because each character including the/is a separate parameter and a call to the method.



An example use of this method is shown below.



The example method is below. Note when building a utility method such as the one below, make sure you are using UTF-16 "Little-Endian" values. The 4D Char function on Mac and Windows using UTF-16 "Little-Endian" values. One of the most usefull aids in finding the UTF-16 value of a UNICODE character is on the web site UT UNICODE character table.

If (True)
   If (False)
      Begin SQL
         /*
          Name: UNICODE_Fractions
          Path: UNICODE_Fractions

          Purpose: Build a fraction string using UNICODE Superscript and Subscript Chars
         */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
   C_STRING(1;${1})
   C_TEXT($0;$Buf_T)
    //--------------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------------
    //local_variable_declarations
   C_LONGINT($Ndx;$Idx;$SOA;$RIS;$Params_L)
   C_BOOLEAN($Numerator_B)
End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters
$Buf_T:=""
$Idx:=0
$Numerator_B:=True

For ($Ndx;1;$Params_L)
   If (${$Ndx}="/")
      $Idx:=$Ndx
      $Ndx:=$Params_L
   End if
End for

//======================== Method Actions ==================================

If (Asserted($Idx>1;"Fraction malformed"))
   For ($Ndx;1;$Params_L)
      If ($Numerator_B)
         Case of
            : (${$Ndx}="/")
            $Buf_T:=$Buf_T+Char(0x2044)
            $Numerator_B:=False

            : (${$Ndx}="0")
            $Buf_T:=$Buf_T+Char(0x2070)

            : (${$Ndx}="1")
            $Buf_T:=$Buf_T+Char(0x00B9)

            : (${$Ndx}="2")
            $Buf_T:=$Buf_T+Char(0x00B2)

            : (${$Ndx}="3")
            $Buf_T:=$Buf_T+Char(0x00B3)

            : (${$Ndx}="4")
            $Buf_T:=$Buf_T+Char(0x2074)

            : (${$Ndx}="5")
            $Buf_T:=$Buf_T+Char(0x2075)

            : (${$Ndx}="6")
            $Buf_T:=$Buf_T+Char(0x2076)

            : (${$Ndx}="7")
            $Buf_T:=$Buf_T+Char(0x2077)

            : (${$Ndx}="8")
            $Buf_T:=$Buf_T+Char(0x2078)

            : (${$Ndx}}="9")
            $Buf_T:=$Buf_T+Char(0x2079)

         Else
            ASSERT(False;"Unknown case test (Superscript)!")

         End case
      Else
         Case of
            : (${$Ndx}="0")
            $Buf_T:=$Buf_T+Char(0x2080)

            : (${$Ndx}="1")
            $Buf_T:=$Buf_T+Char(0x2081)

            : (${$Ndx}="2")
            $Buf_T:=$Buf_T+Char(0x2082)

            : (${$Ndx}="3")
            $Buf_T:=$Buf_T+Char(0x2083)

            : (${$Ndx}="4")
            $Buf_T:=$Buf_T+Char(0x2084)

            : (${$Ndx}="5")
            $Buf_T:=$Buf_T+Char(0x2085)

            : (${$Ndx}="6")
            $Buf_T:=$Buf_T+Char(0x2086)

            : (${$Ndx}="7")
            $Buf_T:=$Buf_T+Char(0x2087)

            : (${$Ndx}="8")
            $Buf_T:=$Buf_T+Char(0x2088)

            : (${$Ndx}="9")
            $Buf_T:=$Buf_T+Char(0x2089)

         Else
            ASSERT(False;"Unknown case test (Subscript)!")

         End case

      End if
   End for
End if

//======================== Clean up and Exit =================================

$0:=$Buf_T