简体   繁体   English

如何将一个浮点数乘以十进制并获得一个浮点数

[英]How to multiply one floating point number by a decimal and get a floating point number

I'm trying to do some math in python. 我正在尝试在python中做一些数学运算。 Here's my code: 这是我的代码:

if(height.is_integer()):
    height = int(height)
meters = height * .3048
in_m = "%d meters" % meters
print "You are also also %s tall (let's hope it doesn't round)." % (in_m)

As you can see I'm trying to make height any user-defined amount and then multiply that by a decimal to get a floating point number defined as meters. 如您所见,我试图将高度设置为任何用户定义的量,然后将其乘以十进制以获得定义为米的浮点数。 The problem is that when I input a number like 6 for height or 5.25 it just rounds down to one meter. 问题是,当我输入数字6作为高度或5.25时,它会四舍五入到一米。 Can anyone help me? 谁能帮我?

in_m = "%d meters" % meters

it just rounds down to one meter. 它只是向下舍入到一米。

Your conversion using %d , applied to any numeric value, prints only its integer part. 使用%d转换(应用于任何数值)仅输出其整数部分。 To print a value as floating one, you should use %f , %e or %g ; 要将值打印为浮点值,应使用%f%e%g for your need, %g seems to be the best default. 根据您的需要, %g似乎是最好的默认设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM