How do I round the decimals to one digit in python?

huangapple go评论78阅读模式
英文:

How do I round the decimals to one digit in python?

问题

I have written a code using which I have to duplicate a statement 100 times. In that, the variable "degrees" has to be iterated by 3.6. Instead of getting a decimal number with 1 decimal digit, I get something like this:

95%{background-color: linear-gradient(342.0deg, red, blue)}
96%{background-color: linear-gradient(345.6deg, red, blue)}
97%{background-color: linear-gradient(349.2deg, red, blue)}
98%{background-color: linear-gradient(352.8deg, red, blue)}

Here is my code:

degrees = 0.0
percent = 0

for i in range(101):
    print(str(percent) + "%{background-color: linear-gradient(" + str(degrees) + "deg, red, blue)}")
    percent += 1
    degrees += 3.6

I want the code to return the result with one decimal point.

英文:

I have written a code using which I have to duplicate a statement 100 times. In that, the variable "degrees" has to be iterated by 3.6. Instead of getting a decimal number with 1 decimal digit, I get something like this:

95%{background-color: linear-gradient(342.0000000000002deg, red, blue)}
96%{background-color: linear-gradient(345.60000000000025deg, red, blue)}
97%{background-color: linear-gradient(349.2000000000003deg, red, blue)}
98%{background-color: linear-gradient(352.8000000000003deg, red, blue)}

Here is my code:

degrees = 0
percent = 0

for i in range(101):
    print(str(percent) + "%{background-color: linear-gradient(" + str(degrees) + "deg, red, blue)}")
    percent += 1
    degrees += 3.6

I want the code to return the result with one decimal point.

答案1

得分: 1

0%{background-color: linear-gradient(0.0度, 红色, 蓝色)}
1%{background-color: linear-gradient(3.6度, 红色, 蓝色)}
2%{background-color: linear-gradient(7.2度, 红色, 蓝色)}
3%{background-color: linear-gradient(10.8度, 红色, 蓝色)}
4%{background-color: linear-gradient(14.4度, 红色, 蓝色)}
5%{background-color: linear-gradient(18.0度, 红色, 蓝色)}
6%{background-color: linear-gradient(21.6度, 红色, 蓝色)}
7%{background-color: linear-gradient(25.2度, 红色, 蓝色)}
......

英文:

you can use f-format string like this

degrees = 0
percent = 0

for i in range(101):
    print(f"{percent}%{{background-color: linear-gradient({degrees:.1f}deg, red, blue)}}")
    percent += 1
    degrees += 3.6

the output is

0%{background-color: linear-gradient(0.0deg, red, blue)}
1%{background-color: linear-gradient(3.6deg, red, blue)}
2%{background-color: linear-gradient(7.2deg, red, blue)}
3%{background-color: linear-gradient(10.8deg, red, blue)}
4%{background-color: linear-gradient(14.4deg, red, blue)}
5%{background-color: linear-gradient(18.0deg, red, blue)}
6%{background-color: linear-gradient(21.6deg, red, blue)}
7%{background-color: linear-gradient(25.2deg, red, blue)}
.......

答案2

得分: 0

你可以使用 round(number, number digit)

for i in range(101):
    print(str(percent) + "%{background-color: linear-gradient(" + str(round(degrees,1)) + "deg, red, blue)}")

如果你的小数严格大于5,它将四舍五入到下一个(ceil)整数,否则它将四舍五入到 floor(floor)。

print(round(342.14,1)) # 342.1
print(round(342.15,1)) # 342.1
print(round(342.16,1)) # 342.2
英文:

You can use round(number, number digit)

for i in range(101):
    print(str(percent) + "%{background-color: linear-gradient(" + str(round(degrees,1)) + "deg, red, blue)}")

If your decimal number is strictly superior to 5, it will round to the next (ceil) whole number, otherwise it will round off to floor (floor).

print(round(342.14,1)) # 342.1
print(round(342.15,1)) # 342.1
print(round(342.16,1)) # 342.2

huangapple
  • 本文由 发表于 2023年7月18日 11:01:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定