Printing ‘single-character’ fractions

Tuesday 9 December 2014 @ 9:47 am

A few years a back I posted a formula for converting a decimal value into a fraction. But the output used full size characters combined with a slash. Recently a customer wanted me to shrink the fraction so it would look like a true fraction.

At first I started looking for super script and subscript characters. Then I found that several fonts have several common fractions as a single character. For instance the Unicode value CHRW(8541) is the character for the fraction symbol “5/8” in several fonts. I found single-character symbols for the 15 most common fractions – those that have a denominator of 2,3,4,5,6 or 8. The formula below will tap into these characters for the corresponding decimal values. I then extended it to include the eight odd numerators over 16. These I created with other special characters that seem to be designed for creating fractions.

But there are several things you have to keep in mind with this technique:

1) Rounding
You have to decide how precise the decimal match has to be. For instance the formula will always print 5/8 if the decimal is .625 but what if it is close, like .62485? I set the rounding in this example to 3 decimals but you can adjust that by changing the ‘D’ variable in the first line. This number should match the the number of decimals in the field you are converting. You may even need to round the incoming value before using that in this formula. Including more decimal places requires a more precise match.

2) Other fractions
What should the formula do if the decimal value doesn’t match one of the 24 fractions in the formula? If that would be considered a mistake in the data then you can print an error message after the final ‘else’. If that would be rare but legitimate you might want to include the original (full size) fraction formula in the report. You can then refer to the full size formula in the final else, making it the last resort.

3) Font
Don’t forget that you have to format this field with one of the fonts that support these symbols. The ones that I found to work the best in my environment are Calibri, Cambria, Khmer UI, Segoe UI, MS Yahei amd Meiryo. You can test others and see how they look if you don’t have these.

 

Local NumberVar D := 3; //Number of decimals to round before comparing

if {@input} = 0 then '' else 

if {@input} = round (1/4 , D) then CHRW(188) else 
if {@input} = round (1/2 , D) then CHRW(189) else 
if {@input} = round (3/4 , D) then CHRW(190) else 

if {@input} = round (1/8 , D) then CHRW(8539) else 
if {@input} = round (3/8 , D) then CHRW(8540) else 
if {@input} = round (5/8 , D) then CHRW(8541) else 
if {@input} = round (7/8 , D) then CHRW(8542) else 

if {@input} = round (1/5 , D) then CHRW(8533) else 
if {@input} = round (2/5 , D) then CHRW(8534) else 
if {@input} = round (3/5 , D) then CHRW(8535) else 
if {@input} = round (4/5 , D) then CHRW(8536) else 

if {@input} = round (1/3 , D) then CHRW(8531) else 
if {@input} = round (2/3 , D) then CHRW(8532) else 

if {@input} = round (1/6 , D) then CHRW(8537) else 
if {@input} = round (5/6 , D) then CHRW(8538) else 

if {@input} = round ( 1/16 , D) then CHRW(185)  & CHRW(8725) & CHRW(8321) & CHRW(8326)  else 
if {@input} = round ( 3/16 , D) then CHRW(179)  & CHRW(8725) & CHRW(8321) & CHRW(8326)  else 
if {@input} = round ( 5/16 , D) then CHRW(8309) & CHRW(8725) & CHRW(8321) & CHRW(8326)  else 
if {@input} = round ( 7/16 , D) then CHRW(8311) & CHRW(8725) & CHRW(8321) & CHRW(8326)  else 
if {@input} = round ( 9/16 , D) then CHRW(8313) & CHRW(8725) & CHRW(8321) & CHRW(8326)  else 
if {@input} = round (11/16 , D) then CHRW(185)  & CHRW(185)  & CHRW(8725) & CHRW(8321) & CHRW(8326)  else 
if {@input} = round (13/16 , D) then CHRW(185)  & CHRW(179)  & CHRW(8725) & CHRW(8321) & CHRW(8326)  else 
if {@input} = round (15/16 , D) then CHRW(185)  & CHRW(8309) & CHRW(8725) & CHRW(8321) & CHRW(8326)  else 

//This refers to a separate formula for full size fractions. 
{@fraction full size} 
// Other optional values:
//"Other Fraction"
//Totext ({@input},4)
(For examples of my most popular formulas, please visit the FORMULAS page on my website.)







Leave a Reply

Jeff-Net
Recrystallize Pro

Crystal Reports Server