HTML::Fraction - Encode fractions as HTML entities


  my $fraction = HTML::Fraction->new;
  print $fraction->tweak($html);


The HTML::Fraction encodes fractions as HTML entities. Some very common fractions have HTML entities (eg 1/2 is ½). Additionally, common vulgar fractions have Unicode characters (eg 1/5 is ⅕). This module takes a string and encodes fractions as entities: this means that it will look pretty in the browser.

Fractions that are supported:

  1/4, 1/2, 3/4, 0/3, 1/3, 2/3, 1/5, 2/5, 3/5,
  4/5, 1/6, 5/6, 1/7, 1/8, 3/8, 5/8, 7/8, 1/9, 1/10

Fractions may be in the string in the form numerator slash denominator (1/5) or in decimal form (0.5). Numbers must be equal to the fraction to two decimal places. This module supports converting whole and fractional decimal numbers (e.g. 2.25.)

For sanity the decimal 0 will not be turned into the unicode for 0/3



The constructor takes no arguments:

  my $fraction = HTML::Fraction->new;



Encode the fractions in the HTML as HTML entities:

  print $fraction->tweak($html);


Encode the fractions that are in the form "1/3" or "5/6" in the HTML as HTML entities, but not decimal fractions of the form "0.5".


Encode the fractions that are in the form "0.5" or "0.5" in the HTML as HTML entities, but not fractions of the form "1/2" or "1/3".


Leon Brocard, <>. Mark Fowler <mark@twoshortplanks> added some code, and probably some bugs.


Copyright (C) 2005, 2012 Leon Brocard

This module is free software; you can redistribute it or modify it under the same terms as Perl itself.


We don't perform normalisation of the denominator and numerator so "4/6" is not converted like 2/3 is. This is intentional.

2.25 doesn't render to the same thing as 2 1/2 (the latter has a space between the digit 2 and the fraction.)

1/7 doesn't represent as a reoccuring pattern of digits in decimal therefore we don't check that digits after 0.14285714 are correct, and assume that they are.