如何在Python 3.5中选择小数点后的第一个数字?

时间:2017-05-07 07:39:20

标签: python python-3.x

我正在学习Python(目前为止喜欢它)并制作了一个华氏温度/摄氏温度转换器。

这是运行时的输出:

  

请输入华氏度或摄氏度以转换:32

     

32.0摄氏度是89.6华氏度。

     

32.0华氏度是0.0摄氏度。

     

你想再次计算吗? (Y / N):

我想要的是这样,除非小数点后面的尾随数字是0(整数),我想完全放弃.0(即5.0到5)。我猜我想要一个if语句来测试它是否等于零但是我该如何选择那个值呢?

完整代码:

def pow(x: Double, y: Double): Double

6 个答案:

答案 0 :(得分:1)

您可以按如下方式获取数字的小数部分:

colors[i]

This帖子探讨了一个类似的问题。

答案 1 :(得分:0)

/* Setting STRUCTURE_SIZE_BOUNDARY to 32 produces more efficient code, but the
value set in previous versions of this toolchain was 8, which produces more
compact structures.  The command line option -mstructure_size_boundary=<n>
can be used to change this value.  For compatibility with the ARM SDK
however the value should be left at 32.  ARM SDT Reference Manual (ARM DUI
0020D) page 2-20 says "Structures are aligned on word boundaries".
The AAPCS specifies a value of 8.  */
#define STRUCTURE_SIZE_BOUNDARY arm_structure_size_boundary

<强>输出

args = [89.6, 32.0, 5.5, 10.0, 9.1]

for var in args:
    if var == int(var):
        print int(var) # prints the number without the decimal part
    else:
        print var

答案 2 :(得分:0)

如果不向现有代码添加任何代码行,您可以使用g类型在format()调用中完成此操作

'{0:g}'.format(5.5)

5.5

'{0:g}'.format(5.0)

5

答案 3 :(得分:0)

<强>设置

ns = [32,32.0,32.04,32.05,32.1,32.45,32.5,32.51,32.6]

<强>解决方案

for n in ns:
    print('Before: {:.1f}'.format(n))
    #conditionally set the decimal place.
    print('After: {:.{}f}'.format(n, 0 if n%1 <0.05 else 1 ))

Before: 32.0
After: 32
Before: 32.0
After: 32
Before: 32.0
After: 32
Before: 32.0
After: 32
Before: 32.1
After: 32.1
Before: 32.5
After: 32.5
Before: 32.5
After: 32.5
Before: 32.5
After: 32.5
Before: 32.6
After: 32.6

答案 4 :(得分:0)

您可以使用模运算符:

for num in [67.89, 123.0]:
    if num % 1 == 0:
        print(int(num))
    else:
        print(num)

#Output:
67.89
123

答案 5 :(得分:0)

试试这个。我已经定义了一个从数字中删除.0s的函数。打印时我将{:.1f}更改为{},因为它会将其格式化为浮点数,并且会有一个小数。

answer = "ERROR"

def remove0(initialValue):
  strValue = str(initialValue)

  if ("." in strValue):
    if(int(str(strValue.split(".")[1])) == 0):
      return int(strValue.split(".")[0])
    else:
      return initialValue
  else:
    return initialValue

def calcfc():
    """ Calculates F to C and C to F, prints out,    and asks if user wants to run again """
    try:
        degrees = remove0(float(input("\nPlease enter the degrees in Fahrenheit or Celsius to convert: ")))
    except Exception:
        input("\nEnter a valid number next time. Hit enter to terminate.")
        exit()

    ftoc = (degrees - 32) * 5 / 9
    ctof = (degrees * 9) / 5 + 32

    ftoc = remove0(ftoc)
    ctof = remove0(ctof)

    print("\n{} degrees Celsius is {} degrees Fahrenheit.".format(degrees, ctof))
    print("{} degrees Fahrenheit is {} degrees Celsius.".format(degrees, ftoc))
    global answer
    answer = input("\n\nDo you want to calculate again? (y/n): ")

calcfc()

# run again?
while answer != "y" and answer != "n":
    answer = input("\nPlease enter y for yes or n for no: ")
while answer == "y":
    calcfc()
if answer == "n":
    exit()