in reply to Unicode vulgar fraction composition
G'day raygun,
As ++ikegami has explained, and you have accepted, there is no compatibility composition.
You have asked about modules. There are some available but I can't say whether they are suitable for your purposes (as you haven't explained that part). Here's a couple. If these aren't suitable, search MetaCPAN using terms reflecting your use case.
"... write my own function to handle these fractions (there are only a dozen or so), ..."
There's actually 18 in total. Three have the codepoints U+00BC - U+00BE and can be found in the PDF Code Chart "C1 Controls and Latin-1 Supplement". The other 15 have the codepoints U+2150 - U+215E and can be found in the PDF Code Chart "Number Forms".
Writing your own function is pretty easy. I wrote one just for the fun of it: I've put it in a spoiler so as not to spoil your fun if you wanted to do this, but do feel free to look and take any code or ideas you want.
#!/usr/bin/env perl use strict; use warnings; use open OUT => qw{:encoding(UTF-8) :std}; use constant VF => { 1 => { 2 => 0xbd, 3 => 0x2153, 4 => 0xbc, 5 => 0x2155, 6 => 0x2159, 7 => 0x2150, 8 => 0x215b, 9 => 0x2151, 10 => 0x2152, }, 2 => { 3 => 0x2154, 5 => 0x2156, }, 3 => { 4 => 0xbe, 5 => 0x2157, 8 => 0x215c, }, 4 => { 5 => 0x2158, }, 5 => { 6 => 0x215a, 8 => 0x215d, }, 7 => { 8 => 0x215e, }, }; my @tests = ( [1,2], [1,3], [1,4], [1,10], [3,8], [9,10], [qw{x y}] ); compose_vulgar_fraction(@$_) for @tests; sub compose_vulgar_fraction { my ($num, $denom) = @_; my $composed; if (exists VF->{$num} and exists VF->{$num}{$denom}) { $composed = chr VF->{$num}{$denom}; } else { $composed = join '/', $num, $denom; } printf "%3s/%-3s : %s\n", $num, $denom, $composed; return; }
Outputs:
1/2 : ½ 1/3 : ⅓ 1/4 : ¼ 1/10 : ⅒ 3/8 : ⅜ 9/10 : 9/10 x/y : x/y
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Unicode vulgar fraction composition
by raygun (Scribe) on Sep 24, 2020 at 17:28 UTC |