Exception to Number Format Exception with “D” and “F”?

后端 未结 3 1816
不知归路
不知归路 2021-01-17 12:42

I have run into a very strange problem in my code. I have a simple temperature converter where the user enters the temperature in Celsius and, after pressing \"Convert\", th

相关标签:
3条回答
  • 2021-01-17 13:15

    some languages allow you to put letters after number literals to signify what type it is.

    if you simply wrote 12.3, it might not know whether it is a float or a double(or it'd have to infer or cast it).

    Your number parser must be picking up on these letters.

    • 12.3d is 12.3 as a double
    • 12.3f is 12.3 as a float
    0 讨论(0)
  • 2021-01-17 13:19

    Java interprets numbers like 123f as referring to a float and 123d as a double, whereas plain 123 means an int.

    0 讨论(0)
  • Read the documentation on the number format supported by parseDouble. The f and d are instances of FloatTypeSuffix.

    0 讨论(0)
提交回复
热议问题