使用unicode粗俗分数创建紧凑/人性化的花车 [英] Create compact/human-friendly floats using unicode vulgar fractions

查看:182
本文介绍了使用unicode粗俗分数创建紧凑/人性化的花车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何模块(最好在标准库中),可以将一个浮点数变成更人性化的东西?也许这不是更人性化,但至少更紧凑。



例如。 4.625会变成4bb $ p







$ b 大纲是最好的,我可以拿出:

pre code进口unicodedata
$ b $ def simpl_float(数字):
vf =VULGAR FRACTION
vulgars = {0.125:unicodedata.lookup(vf +ONE EIGHTH),
0.2:unicodedata.lookup(vf +ONE FIFTH),
0.25:unicodedata.lookup(vf +ONE QUARTER),
0.375:unicodedata.lookup(vf +THREE EIGHTHS),
0.4:unicodedata.lookup(vf +TWO FIFTHS ),
0.5:unicodedata.lookup(vf +ONE HALF),
0.6:unicodedata.lookup(vf +THREE FIFTHS),
0.625:unicodedata.lookup(vf +FIVE EIGHTHS),
0.75:unicodedata.lookup(vf +THREE QUARTERS),
0.8:unicodedata.lookup(vf +FOUR FIFTHS),
0.875:unicodedata.lookup(vf +SEVEN EIGHTHS)}

decimal = int(number)
if number == decimal:
return unicode(decimal)

低俗= vulgars.get(数字 - 小数)
如果低俗:
如果小数== 0:
返回低俗
返回%d%s% (十进制,低俗)
返回%.1f%number

使用unicodedata模块非常容易,但我找不到一个好的方法来创建这些字符串。

解决方案

Unicode中只有二十个这样的分数形式。这是不太可能的(因为它们只是为了向后兼容其他字符集),所以对它们进行硬编码可能足够强大。



编码分数的一般方法是使用 / U + 2044 FRACTION SLASH。字体整形/布局引擎允许用分数斜线(例如1/2)作为倾斜的或堆叠的分数(例如1/2)来呈现数字 - 但是,我还没有遇到任何这样做(甚至简单的渲染不幸的是相当丑陋)。

 进口数学
进口分数

VULGAR_FRACTIONS = {(5 ,8):'\\\⅝',...}

def compact_float(number):
parts = math.modf(number)
fraction = fractions.Fraction部分[0])
simple =(fraction.numerator,fraction.denominator)
form = VULGAR_FRACTIONS.get(simple)
return'%i%s'%(parts [1],表单)如果表单其他str(数字)

目前还不清楚处理精度的最好方法是什么例如, 1/3 ),因为它取决于你的数字存在的格式和可接受的错误数量。


Are there any modules (preferably in the standard library), that can turn a float number into something the is more human friendly? Maybe it's not more human friendly, but at least more compact.

eg. 4.625 would become "4⅝"

(Bonus brownie points for recognizing pi to a reasonable precision)

This code outline was the best I could come up with:

import unicodedata

def simplify_float(number):
    vf = "VULGAR FRACTION "
    vulgars = {0.125 : unicodedata.lookup(vf + "ONE EIGHTH"),
               0.2   : unicodedata.lookup(vf + "ONE FIFTH"),
               0.25  : unicodedata.lookup(vf + "ONE QUARTER"),
               0.375 : unicodedata.lookup(vf + "THREE EIGHTHS"),
               0.4   : unicodedata.lookup(vf + "TWO FIFTHS"),
               0.5   : unicodedata.lookup(vf + "ONE HALF"),
               0.6   : unicodedata.lookup(vf + "THREE FIFTHS"),
               0.625 : unicodedata.lookup(vf + "FIVE EIGHTHS"),
               0.75  : unicodedata.lookup(vf + "THREE QUARTERS"),
               0.8   : unicodedata.lookup(vf + "FOUR FIFTHS"),
               0.875 : unicodedata.lookup(vf + "SEVEN EIGHTHS")}

    decimal = int(number)
    if number == decimal:
        return unicode(decimal)

    vulgar = vulgars.get(number - decimal)
    if vulgar:
        if decimal == 0:
            return vulgar
        return "%d%s" % (decimal, vulgar)
    return "%.1f" % number

Going the other direction is pretty easy using the unicodedata module, but I couldn't find a good way to author these strings in the first place.

解决方案

There are only twenty of these fraction forms in Unicode. It is unlikely that there will ever be more (as they only exist for backwards compatibility with other character sets), so hardcoding them is probably robust enough.

The general way of encoding fractions is to use U+2044 FRACTION SLASH. Font shaping/layout engines are allowed to render numbers with a fraction slash (e.g., 1⁄2) with as a slanted or stacked fraction (e.g., ½)—however, I have not encountered any that do (and even the plain rendering is unfortunately quite ugly).

import math
import fractions

VULGAR_FRACTIONS = {(5, 8) : '\u215D', ...}

def compact_float(number):
    parts = math.modf(number)
    fraction = fractions.Fraction(parts[0])
    simple = (fraction.numerator, fraction.denominator)
    form = VULGAR_FRACTIONS.get(simple)
    return '%i%s' % (parts[1], form) if form else str(number)

It's not clear what the best way is to handle precision (e.g., 1/3) as it will depend on the format your numbers exist in and how much error is acceptable.

这篇关于使用unicode粗俗分数创建紧凑/人性化的花车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆