【问题标题】:Formating decimal and digit together一起格式化小数和数字
【发布时间】:2020-04-01 08:08:48
【问题描述】:

我想将学位转换为 dms。我写了一个代码,但输出不是我想要的。

我也想要 2 位数秒。我想要 03.56895 而不是 3.56895。

我的代码:

def degree2dms(degree):
    int_degree = int(degree)
    degree_decimal = degree - int_degree
    min_part = degree_decimal *60
    min = int(min_part)
    sec_part = min_part-min
    sec = sec_part *60
    if sec == 60:
        sec =0
        min+=min
    if min ==60:
        min = 0    
        degree += degree

    sec = round(sec,5)

    return "%03d°%02d'%02.5f''"% (int_degree,min,sec)

输出它给出:036°06'8.54578'', 034°03'5.55987''

我想要的输出:036°06'08.58975'', 034°03'05.58900''

【问题讨论】:

    标签: python format decimal


    【解决方案1】:

    尝试使用:

    return "%03d°%02d'%08.5f''"% (int_degree,min,sec)
    

    说明

    %08.5f 将使用填充零 (0) 格式化您的 float,总共有 8 数字(包括 .-),并在小数点后显示 5 数字。

    【讨论】:

      猜你喜欢
      • 2019-05-07
      • 2012-12-14
      • 2011-04-27
      • 2012-05-07
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多