-2

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.

InSync
  • 4,851
  • 4
  • 8
  • 30
sakrockzx
  • 1
  • 1
  • 1
  • 1
    Does this answer your question? [Round float to x decimals?](https://stackoverflow.com/questions/13479163/round-float-to-x-decimals) – Mark Jul 18 '23 at 03:10
  • @Barmar Not the best solution. `round(degrees, 1)` yields a floating-point value that's only an approximation of the rounded value. For example, `round(1.2, 1)` yields exactly ``1.1999999999999999555910790149937383830547332763671875`. Round when converting to a string or on output. Rounding a binary floating-point value to a number of *decimal* digits is rarely useful. – Keith Thompson Jul 18 '23 at 03:14
  • May I ask why are you using Python to generate CSS code? – InSync Jul 18 '23 at 03:28
  • @InSync Maybe because they can. Do you have a feeling that there is something wrong with that? – Matthias Jul 18 '23 at 06:51
  • @Matthias Somewhat. If it comes to generating CSS code, a CSS preprocessor is most often the answer. Not to mention, `linear-gradient()` is not a valid value for `background-color`. – InSync Jul 18 '23 at 07:01

2 Answers2

1

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)}
.......
Xiaomin Wu
  • 400
  • 1
  • 5
0

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
huangkaiyi
  • 73
  • 2
  • 9