Dylan Wheeler
Dylan Wheeler

Reputation: 7074

Java Remove all cases of .0

I want a filter to run through a string and eliminate all of the un-needed .0's from the doubles. I have already tried replaces, but cases like 8.02 turn into 82.

I have a feeling this has been done before, but I cannot find any help.

I am looking for a method that will take in a String, example: "[double] plus [double] is equal to [double]", where the [double] is a double that will need to be checked for the redundant decimal.

Thanks in advance!

Upvotes: 2

Views: 4877

Answers (5)

davogotland
davogotland

Reputation: 2777

let's say you have a string called s that contains text like you described. simply do:

s = s.replaceAll("([0-9])\\.0+([^0-9]|$)", "$1$2");

and you're done.

ok, so i edited this a couple of times. now it works though!

Upvotes: 2

Lion
Lion

Reputation: 19027

The following program will remove all trailing zeros from the fractional part of a double value. If your requirements are somewhat different, you may need to modify it slightly.

final public class Main
{
    public static void main(String...args)
    {
        System.out.println("Enter how many numbers you want to check:->");
        Scanner scan = new Scanner(System.in);

        final double KEY_VALUE = 0.0;
        final Double TOLERANCE = 0.000000000001d;

        int n = scan.nextInt();
        double[] a = new double[n];
        for (int i = 0; i < n; i++)
        {
                a[i] = scan.nextDouble();
        }

        List<Double> newList = new ArrayList<Double>();
        for (int k = 0; k < n; k++)
        {
            if (Math.abs(a[k] - KEY_VALUE) < TOLERANCE)
            {
                    continue;
            }
            newList.add(a[k]);
        }

        System.out.println(newList);
    }
}

You can specifically use DecimalFormat to truncate the specified number of decimal places, if you need such as follows.

double x=10.4555600500000;
DecimalFormat df=new DecimalFormat("#.##");
System.out.println(df.format(x));

Would return 10.46.

Upvotes: 0

Daryl Teo
Daryl Teo

Reputation: 5495

The other posts assume you're working with a short string containing only decimal. Assuming you're working with large strings of text, you can use Pattern/Matcher classes (I'm at work, so posting in a hurry. Check for errors)

Use this regex to replace:

/* >1 digits followed by a decimal and >1 zeros. Note capture group on first set of digits. This will only match decimals with trailing 0s, and not 8.0002 */
(\d+)\.0+

Replace with

/* First capture group */
$1

I'm unsure of the regex rules for Java, so use this as a concept to get what you want.

Upvotes: 0

litterbugkid
litterbugkid

Reputation: 3666

Can you not take away the trailing zeros before you construct them into your string? This way you could use the DecimalFormatter method someone posted earlier and then deleted.

NumberFormat formatter = new DecimalFormat("0.##"); 
String str = formatter.format("8.0200"); 
System.out.println(str);

Give them credit for the code if they come back.

Upvotes: 0

Stephen P
Stephen P

Reputation: 14800

You can do this with a java.text.DecimalFormat object.

Upvotes: 0

Related Questions