I want to change it to a string without any unnecessary parts such as zero below the decimal point when I print out the real number, what should I do?

Asked 1 years ago, Updated 1 years ago, 119 views

On 64-bit operating systems, double represents up to +-253.

Until 53

I'm using double because I think the size of the number I'm expressing is about unsigned 32-bit Integer. The problem is that even if you express an integer like the example below, it comes out like a real number.

When I put it in String, I put it in the same format as String.format("%f", value). All I want is

232
0.18
1237875192
4.58
0
1.2345

The problem is like this

232.00000000
0.18000000000
1237875192.0
4.5800000000
0.00000000
1.23450000

It's printed like this. Of course, we can create a function to get rid of the zero after that, but I can't do it because I'm afraid that the performance will decrease due to string computation. Is there any other good way?

string java floating-point format double

2022-09-22 22:29

1 Answers

public static String fmt(double d)
{
    if(d == (long) d)
        return String.format("%d",(long)d);
    else
        return String.format("%s",d);
}

If double is saved as an Integer, you can change it to the Integer format and if not, you can use the method of putting it in as it is.


2022-09-22 22:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.