zoukankan      html  css  js  c++  java
  • Friendly number

    Friendly number

    Long numbers can be made to look nicer, so let’s write some code to do just that.

    You should write a function for converting a number using several rules. For starters, you will need to cut the number with a given base (base argument; default 1000). The number should be a coefficient with letters designating the power. The coefficient is a real number with decimal after the point (decimals argument; default 0). You will be given a list of power designations (powers argument; default ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']). If you are given suffix (suffixargument; default ‘’) , then you must be add it at the end. For the coefficient, use the rounding down rule (5.6⇒5, -5.6⇒-5) if the decimal = 0, else use the standard rounding procedure. If you don’t have enough powers - stay at the maximum. If the number of decimals are greater than the real number of digits after dot, trail it with zeroes. And zero is always zero without powers.

    Let's look at examples. It will be simpler.

    • n=102
      result: "102", the base is default 1000 and 102 is lower this base.
    • n=10240
      result: "10k", the base is default 1000 and rounding down.
    • n=12341234, decimals=1
      result: "12.3M", one digit after the dot.
    • n=12000000, decimals=3
      result: "12.000M", trailing zeros.
    • n=12461, decimals=1
      result: "12.5k", standard rounding.
    • n=1024000000, base=1024, suffix='iB'
      result: '976MiB', the different base and the suffix.
    • n=-150, base=100, powers=['', 'd', 'D']
      result: '-1d', the negative number and rounding down.
    • n=-155, base=100, decimals=1, powers=['', 'd', 'D']
      result: '-1.6d', the negative number and standard rounding.
    • n=255000000000, powers=['', 'k', 'M']
      result: '255000M', there is not enough powers.

    Input: A number as an integer. The keyword argument "base" as an integer, default 1000. The keyword argument "decimals" as an integer, default 0. The keyword argument "powers" as a list of string, default ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'].

    Output: The converted number as a string.

    原题链接: http://www.checkio.org/mission/friendly-number/

    题目大义: 将给定的数字, 按照传入的参数, 如小数位, base, 幂次, 及后缀

    思路: 熟悉python的数值计算, 在python3之后, /执行的是真正的除法, 通过代码实际运行情况, 发现/的结果均为float型, 无论除数被除数; 另外python3支持大整数, 而在大整数执行/运算时, 结果可能有偏差, 如在本题中

    1 >>> i = 10 ** 32
    2 >>> i /= 1000
    3 >>> i
    4 1e+29
    5 >>> i /= 1000
    6 >>> i
    7 9.999999999999999e+25

    以上为在我电脑上python3.4.1的运行结果

    可以通过使用Decimal解决这个问题, 代码如下

     1 from decimal import Decimal
     2 
     3 def friendly_number(number, base=1000, decimals=0, suffix='',
     4                     powers=['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']):
     5     """
     6     Format a number as friendly text, using common suffixes.
     7     """
     8     step = 0
     9     powers_len = len(powers) - 1
    10 
    11     number = Decimal(number)
    12 
    13     while abs(number) >= base and step < powers_len:
    14         step += 1
    15         number /= base
    16 
    17     if decimals != 0:
    18         coefficent = round(number, decimals)
    19     else:
    20         coefficent = int(number)
    21 
    22     return '%.*f' % (decimals, coefficent) + powers[step] + suffix

    注意最后一行的写法, 保留小数点位数的形式与c语言类似

    review Sim0000's codes

     1 def friendly_number(number, base=1000, decimals=0, suffix='',
     2                     powers=['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']):
     3     # At first, decompose the number to value and exponent.
     4     e = 0
     5     while e + 1 < len(powers) and abs(number) >= base ** (e + 1) : e += 1
     6     number /= base ** e
     7     # Then round it.
     8     number = round(number, decimals) if decimals else int(number)
     9     # At last, Format it.
    10     return '{:.0f}'.replace('0', str(decimals)).format(number) + powers[e] + suffix

    思路一致, 注意第10行的用法, 冒号后是格式控制

     
  • 相关阅读:
    DAY 223 GIT
    swooleHTTP
    swooleWebSocket
    swooleUDP
    swoole异步MySql
    swooleTCP
    谈谈继承的局限性
    也谈过程决定质量
    谁该为参数负责
    使用function改进设计
  • 原文地址:https://www.cnblogs.com/hzhesi/p/3914071.html
Copyright © 2011-2022 走看看