0

I have looking for the solution around and doing a little workaround here, but can not wrap up all to meet my expectations. For example, I have double values as below

double x = 101.00000
double y = 102.02000
double z = 103.20000
double k = 104.30020

I want all the value to become String with the smallest number of decimal they have to show. Expected result

String l = "101"
String m = "102.02"
String n = "103.2"
String o = "104.3002"
// and the l = formatted x, m = formatted y, n = formatted z, o = formatted k

Can this be achieved in any way?

(Note: someone have suggest my question may duplicate with remove trailing zero in java, but I suggest it's different because in my question the base value is double not String)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Fran Na Jaya
  • 280
  • 3
  • 8
  • I think no bro @Torben, my question base value is double so I need to convert the value to String and do the formatting at the same time – Fran Na Jaya Dec 28 '21 at 09:19
  • You do not understand your problem correctly, Fran. Your problem is not about double values. It's about "string representation of double values." Until you understand the difference between those, you probably should try to maintain a repectful attitude to those helping you and refrain from incorrectly referring to them as "bros". – Torben Dec 28 '21 at 11:17
  • I appologize for using terms 'bro'. And thanks a lot @Torben for the critics. I accept your criticism and suggestions. But still I am on my opinion that the root problem of my question and the question you suggest a little bit different. The question you directing me to is about how to reformat a String(which may / always contains a numeric value in decimals or any other) to remove the trailing zero value in it. Thanks a lot. anyone seen this and think I am wrong please notify me too, and I might take it down then. I doesn't want to spread any miss understanding and bad intention. Sorry – Fran Na Jaya Dec 28 '21 at 11:26
  • Please check your answer: https://stackoverflow.com/questions/14984664/remove-trailing-zero-in-java/74925939#74925939 – Subarata Talukder Dec 27 '22 at 05:26

2 Answers2

1

Hope this helps:

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

Input:

1. 101.00000
2. 102.02000

Output:

1. 101
2. 102.02
  • I still have some question. Do in java casting ```(long) val``` will never throw any error?? I see in the implementation it's simply just need to cast the value and compare it to the original value – Fran Na Jaya Dec 28 '21 at 09:12
  • This will fail when val exceeds Long.MAX_VALUE. – Torben Dec 28 '21 at 11:21
-1

CONVERT DOUBLE TO STRING

In Java, if you want to convert a value type to another, you should see this 'another type' class and try to find a converting method.

I this case you want to convert a Double type to String. String class has a method called String.valueOf(double d) that changes the number value to String. It also can convert other number types to String thanks to the overload properties.


REMOVING UNNECESSARY ZERO

Once you have the String values, use this code with each one to remove the unnecessary zeros.

s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");

In your case:

Double x = 101.00000;
Double y = 102.02000;
Double z = 103.20000;
Double k = 104.30020;

String l = String.valueOf(x);
String m = String.valueOf(y);
String n = String.valueOf(z);
String o = String.valueOf(k);

l = l.indexOf(".") < 0 ? l : l.replaceAll("0*$", "").replaceAll("\\.$", "");
m = m.indexOf(".") < 0 ? m : m.replaceAll("0*$", "").replaceAll("\\.$", "");
n = n.indexOf(".") < 0 ? n : n.replaceAll("0*$", "").replaceAll("\\.$", "");
o = o.indexOf(".") < 0 ? o : o.replaceAll("0*$", "").replaceAll("\\.$", "");

Output:

  • 101
  • 102.02
  • 103.2
  • 104.3002
rorsdev
  • 1
  • 2