Python 3.x 小数点后2位python的最少代码行

Python 3.x 小数点后2位python的最少代码行,python-3.x,Python 3.x,将10+小数位转换为2小数位所需的最少行数是多少 我看过十进制和其他代码片段,但它们似乎太冗长了,我觉得必须有一种更简单的方法 我觉得我已经优雅地收紧了我的代码,我的CRENT选项似乎增加了太多 下面是我在当前状态下运行的代码 import requests import json cur_price_url = "http://api.coindesk.com/v1/bpi/currentprice.json" def bitcoin_current_price(url): ""

将10+小数位转换为2小数位所需的最少行数是多少

我看过十进制和其他代码片段,但它们似乎太冗长了,我觉得必须有一种更简单的方法

我觉得我已经优雅地收紧了我的代码,我的CRENT选项似乎增加了太多

下面是我在当前状态下运行的代码

import requests
import json

cur_price_url = "http://api.coindesk.com/v1/bpi/currentprice.json"


def bitcoin_current_price(url):
    """
        Function to return json data from API
        “Powered by CoinDesk” http://www.coindesk.com/price/
    """
    return requests.get(url).json()


btc_price = (bitcoin_current_price(cur_price_url))

with open("btc_price.json", "w+") as jf:
    json.dump(btc_price, jf, indent=4)


def disclaimer():
    """ Returns The API Disclaimer Information as Requested.."""
    print('-' * 160)
    print("""“Powered by CoinDesk” http://www.coindesk.com/price/)""")
    print(btc_price['disclaimer'])
    print('-' * 160)


disclaimer()


def btc_cur():
    """  Displays Different Currency Rates and Information """
    print('\n USD: ' + str(btc_price['bpi']['USD']['rate']))
    print('\n EUR: ' + str(btc_price['bpi']['EUR']['rate']))
    print('\n GBP: ' + str(btc_price['bpi']['GBP']['rate']))


btc_cur()

bitcoin_usd = str(btc_price['bpi']['USD']['rate'])
bitcoin_eur = str(btc_price['bpi']['USD']['rate'])
bitcoin_gbp = str(btc_price['bpi']['USD']['rate'])

amount_input = input(str("Input Amount in USD: "))
amount = float(amount_input)
btc = 1
amount_per_add = 10
currency = bitcoin_usd
hand_charge = 5

def cur_name():
    """ Name the Currencies """
    if currency == bitcoin_usd:
        cur_prt = "USD"
    elif currency == bitcoin_gbp:
        cur_prt = "GBP"
    else:
        cur_prt = "EUR"
    return cur_prt


def convert(cur, num):
    """simple conversion between currencies"""
    e_b = btc / float(cur.replace(",", ""))
    print('\n' + str(1) + " " + str(cur_name()) + " is " + str(e_b) + " BTC")
    print(str(1) + " BTC" + " is " + str(cur) + ' ' + str(cur_name()) + '\n')

    e_b_con = e_b * num
    return e_b_con


con_rtn = convert(currency, amount)

print(str(amount) + ' ' + str(cur_name()) + " is " + str(con_rtn) + " BTC\n")


def add_x_percent(per):
    """ add x % of amount to amount """

    add_per = amount / 100 * per
    with_added = add_per + amount
    print(str(amount_per_add) + ' % of ' + str(amount) +
        ' is ' + str(add_per) + ' ' + str(cur_name()))
    print("Total Price with " + str(amount_per_add) +
        '% commission is: ' + str(with_added)  + ' ' + str(cur_name()) + '\n')
    with_hand_charge = with_added + hand_charge
    print("Total Price PLUS $" + str(hand_charge) + " Handling Charge AND " + str(amount_per_add) +
        '% commission is: ' + str(with_hand_charge)  + ' ' + str(cur_name()) + '\n')
    print("Total Profit is: " + str(add_per + hand_charge) + ' ' + str(cur_name()) + '\n')
    paypal_charge = with_hand_charge + (with_hand_charge / 100 * 5) 
    print("Price including Paypal Charge at 5%: " + str(paypal_charge) + str(cur_name()) + '\n')
    return with_added


add_x_percent(amount_per_add)

我从来没有真正编写过Python,但我认为您希望使用
round
函数

但是,输入是一个字符串,因此您可能需要先将其转换为浮点。这会将字符串转换为浮点:

float(btc_price['bpi']['USD']['rate'])
因此,不是:

str(btc_price['bpi']['USD']['rate'])
使用:


我觉得如果不粘贴所有代码,这个问题可能会变得更加简洁。问题似乎只是关于将数字从一种格式更改为另一种格式,那么为什么我们需要查看所有内容呢?此外,CoinBase API似乎只返回4个小数点处的
速率
,因此您说的“需要将10+小数位转换为2个小数位”似乎都不正确。我向亨利道歉。。我不知道哪段代码是相关的。。不可否认,我投了10+是因为我没有数数数字。。我应该说的是2+@如果你在最初的问题中说出你尝试了什么,以及这些尝试的结果是什么,这会有所帮助。我已经更新了我的答案,包括在四舍五入之前转换成浮点。试一试,让我知道它是否有效。祝你好运。从技术上讲,舍入并不能将值的小数位数减少到给定的小数位数,因为结果是一个浮点数。
str(round(float(btc_price['bpi']['USD']['rate']), 2)