Java: how to output dual type numbers to integer format if the decimal number is 0

I have a dual type array {1.0,2.3,3.45,7.8,9.0,5.0} When outputting numbers, I want numbers with a decimal of 0 to be displayed as integers: for example, 1.0 = > 1,9.0 = > 9

How can you easily do this in Java? thank you

Solution

Do not use any of the libraries I suggest:

public static String parse(double num) {
    if((int) num == num) return Integer.toString((int) num); //for you,StackOverflowException
    return String.valueOf(num); //and for you,Christian Kuetbach
}

Converting double to int truncates the decimal places

Convert numbers into strings in any way at will;)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>