Skip to content
Advertisement

How to print specific number right of the decimal point

1.45
4

Hey everyone i need to print number right of the decimal point like if i input 1.45 it needs to output 4 because its first number after point i also tried multiplying by 10 or 100 but i dont know much numbers will be after point so if there is 3 numbers after point youn still need to print the first number after point. I have tried using “.2f” which didnt work it just made things worse i also tried finding things in internet all i found was about “.2f”.

Advertisement

Answer

You could just multiply by 10, ignore any fractional component by converting to int then print the value modulus 10. Like this:

n = 1.45

print(int(n * 10) % 10)

Output:

4
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement