【问题标题】:replace values in a float with a string- python用字符串替换浮点数中的值-python
【发布时间】:2016-11-11 07:45:33
【问题描述】:

我想用字符串替换浮点数中的点

例如,如果我有一个浮点数 15.444,我需要将其打印为 15 个鸡蛋以下 444 个鸡

如果以我为例,这里是代码

enter code here

name = "chris"
height_cm = 175
height = (height_cm * 1/2.54) * 1/12 
weight = 79


print "He is %s" % name
print "His height is %.2f" %height, "inches" 
print "His weight is %d" % weight

第二个打印行将给出输出“我的身高是 5.7 英寸”,我该如何替换“。”用字符串。在这种情况下,我需要替换“。”带有字符串“feet”

++++++++++++++++++++输出++++++++++++++++++++++++++ 他是克里斯 他的身高是5.7英寸 他的体重是79 +++++++++++++++++++++输出+++++++++++++++++++++++++

【问题讨论】:

  • 在发布之前尝试解决问题。我们不会为您编写代码。
  • 你试过什么?该网站要求您展示您尝试过的内容并解释它是如何失败的。
  • 将其转换为字符串,将其拆分为 dot 。如果您在尝试此操作后遇到任何问题,请在此处寻求帮助。
  • 你最好不要一开始就忙着把它存储为浮点数,除非这是不可避免的。
  • @Eli 对不起,我是这个网站的新手和编码新手,用我尝试过的方法更新了问题。

标签: python


【解决方案1】:

我不会编写 python 代码,但您可能想按照 this(java) 做一些事情:

float input = 15.444f;
int[] split = (input+"").split(".");
System.out.println(split[0]+" eggs and "+split[1]+" chickens");

但是为什么你需要做这样的事情。浮点数是存储 2 个整数值的可怕方法。尝试使用数组。

【讨论】:

  • OP 已将该问题标记为“Python”。所以他想要 Python 中的答案。
  • 另外,这甚至不是有效的java。
猜你喜欢
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 2022-01-16
相关资源
最近更新 更多