2

I have an input which can have a 13 long decimal number with cents and thousands separators. So i can have something like this:

13.213.232.132,13

The same field on my database table is configured to be a NUMBER(13,2) column (oracle)

How can i convert from

13.213.232.132,13 to 13213232132,13

and Vice-Versa?

Joao Victor
  • 1,111
  • 4
  • 19
  • 39

4 Answers4

2

I'd use NumberFormat.parse() -- get an instance of NumberFormat to match your locale, create a string indicating the format expected, then parse the input with it.

arcy
  • 12,845
  • 12
  • 58
  • 103
2

RECOMENDATION

Before all and as a recommendation for future questions you might ask, please read:

How to ask.

How to create a Minimal Complete and Verifiable Example (MCVE).

So you can get better help and faster.

SOLUTION 1

You might want to use DecimalFormat

Here's an example on how to use it.

import java.util.*;
import java.text.*;
class NumberConverter {
    public static void main(String args[]) {
        double number = 12312312312.31;
        String pattern = "###.##";
        String pattern2 = "###,###.##";
        DecimalFormat myFormatter = new DecimalFormat(pattern);
        System.out.println(myFormatter.format(number));
        myFormatter = new DecimalFormat(pattern2);
        System.out.println(myFormatter.format(number));
    }
}

All you need to do with this example is replace , for . and viceversa. (I mean separators).

The actual output for my example is:

12312312312.31
12,312,312,312.31

SOLUTION 2

Here's another option on how to achieve it, code taken from @JonSkeet answer. Setting locale, but again assuming your number is a double.

import java.util.*;
import java.text.*;
class NumberConverter {
    public static void main(String args[]) {
        double number = 13213232132.13;
        NumberFormat f = NumberFormat.getInstance(Locale.GERMANY);
        ((DecimalFormat) f).applyPattern("##0,000.00");
        System.out.println(f.format(number));
    }
}

SOLUTION 3

If your input is a String then this could be another way of solving (Since it's a 13 length number that's why I use those limits).

import java.util.*;
import java.text.*;
import java.lang.StringBuilder;
class NumberConverter {
    public static void main(String args[]) {
        String number = "13.213.232.132,13";
        String number1;
        String number2;
        
        number1 = number.replace(".", "");
        
        int length;
        int length1;
        StringBuilder sb = new StringBuilder();
        
        sb.append(number1.charAt(0));
        sb.append(number1.charAt(1));
        sb.append(".");
        
        int counter = 0;
        for(int i = 2; i < 11; i++) {
            if(counter == 3) {
                sb.append(".");
                counter = 0;
            }
            sb.append(number1.charAt(i));
            counter++;
        }
        sb.append(",");
        sb.append(number1.charAt(12));
        sb.append(number1.charAt(13));
        
        number2 = sb.toString();
        
        System.out.println(number);
        System.out.println(number1);
        System.out.println(number2);
    }
}

SOLUTION 4

As @kaos said in his comment you can also do it by using DecimalFomatSymbols in this way:

import java.util.*;
import java.text.*;
import java.lang.StringBuilder;
class NumberConverter {
    public static void main(String args[]) {
        String formatString = "###,###.##";
        double number = 12312312312.31;
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
        otherSymbols.setDecimalSeparator(',');
        otherSymbols.setGroupingSeparator('.'); 
        DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
        
        System.out.println(df.format(number));
    }
}

Hopefuly any of these examples might help you in whatever you're trying to do.

Good Luck

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • This doesn't answer how to parse the number that has `.` as a thousands separator and `,` as a decimal point. It presumes the number is already in a `double`, which seems a bit unlikely. – David Conrad Aug 07 '14 at 19:11
  • @DavidConrad yeah might not be the exact solution, I'm looking for a "custom" decimal format which allows to do that. OP didn't specify if it was a double or a string. Working on another solution. – Frakcool Aug 07 '14 at 19:24
  • If it was a double, it couldn't have thousands separators in it. – David Conrad Aug 07 '14 at 19:30
  • 1
    Maybe it was an example on how it looks like right now (with a decimal format not specified by OP either) – Frakcool Aug 07 '14 at 19:32
  • You can use DecimalFomatSymbols for changing thousand and decimal characters. – kaos Aug 07 '14 at 19:48
  • @kaos do you have an example? so I can update it with it, and add it with my last solution (when input isn't a double). – Frakcool Aug 07 '14 at 19:52
  • @Frakcool http://stackoverflow.com/questions/5054132/how-to-change-the-decimal-separator-of-decimalformat-from-comma-to-dot-point – kaos Aug 07 '14 at 19:53
  • @kaos check update, not sure if it's what you said :) – Frakcool Aug 07 '14 at 20:43
  • @DavidConrad could you check updates? There's an update when it's not a `double` but a `String` instead. – Frakcool Aug 07 '14 at 20:43
  • Comprehensive! I notice you have "solvind" for "solving" at one place, BTW. – David Conrad Aug 07 '14 at 20:47
  • @Frakcool Yeap that's it. You can eventually change Locale.GERMAN to Locale.getDefault() as it will return the current Locale for JVM instead of switching it to Germany. – kaos Aug 07 '14 at 21:03
  • @kaos oh thanks I also learned from this answer some things C: updated btw – Frakcool Aug 07 '14 at 21:39
1

Use DecimalFormat:

DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(new Locale("pt", "br"));
    formatter.applyPattern("#0.##"); //i guess this is the pattern you need.

    System.out.println(formatter.format(new BigDecimal("13213232132.13")));
squallsv
  • 482
  • 2
  • 11
0

A very ugly approach, maybe it helps:

public static void main(final String[] args) {
    final String str = "12.345.678.912,13";
    final String improved = str.replaceAll("\\.", "").replaceAll(",", ".");
    System.out.println(improved);
    final double dbl = Double.parseDouble(improved);
    System.out.println("For database: " + dbl);
    // You may even pass a Locale to String::format
    final String str2 = String.format("%,f", dbl);
    System.out.println(str2);

    final String stringResult = str2.substring(0, str2.length() - 4);
    System.out.println(stringResult);
}

You may find, what you want to have.

atmin
  • 370
  • 1
  • 7