格式化美元符号和小数对齐

时间:2018-02-05 18:21:51

标签: python formatting format alignment decimal

嗨,我是python的新手,已经在python上玩了一段时间,但我仍然无法得到美元符号和小数对齐。

library(tidyverse)
library(broom)

models <- list(hp ~ exp(cyl), hp ~ cyl)

mtcars %>% 
    list %>%                         # make it a list
    cross2(models) %>%               # get combinations
    transpose %>%                    # put into a nice format
    set_names("data", "formula") %>% # set names to lm arg names
    pmap(lm) %>%                     # fit models
    map_df(tidy)                     # tidy it up

我希望它看起来像这样:

cost=pack*price

discount= cost * dis #the percent discount
amountdue = discount + cost #how much they owe


print(format("Total $", "13"), format(cost,"6.2f"))
print(format("Dis $", "13"), format(discount,"6.2f"))
print(format("Amount Due: $","13"),format(amountdue, "6.2f"))

谢谢!

1 个答案:

答案 0 :(得分:0)

更好的解决方案是使用新的格式样式:

print("{:<13}$ {:>7.2f}".format("Total", 7.34))
print("{:<13}$ {:>7.2f}".format("Dis", 7.34))
print("{:<13}$ {:>7.2f}".format("Amount Due:", 7.34))
# Total        $    7.34
# Dis          $    7.34
# Amount Due:  $    7.34