Link to home
Start Free TrialLog in
Avatar of cofactor

asked on

String format issue

I have two String data this way


Expected Output:

output x="123456,789"
output y="123456,789.09"

What is the  easy way to do this ?
Avatar of Kanti Prasad
Kanti Prasad


To avoid confusion try
String xformatted = x.substring(0,6) + "," + x.substring(6,9)
String yformatted = y.substring(0,6) + "," + y.substring(6,12)
x = xformatted
y = yformatted
Avatar of cofactor


those are sample input.

you should not fix 6 in your code.

input = ="3456,789"
output ="3456,789"

your code wont work...more generic solution required.

idea is to put comma in the 1000th place as shown above

Sorry are you looking to get the length of x and y and then put commas in respective places ?

As I just counted the outputs you want above for x and y and just appended the comma in the position you wanted.  

 x.substring(Start from left position i.e 1,End at 6 place) + "," + x.substring(after appending comma Start from 7 position,Till end i.e 9)
Just an observation, why are you holding a number as a String?

double  x= 123456789
double y= 123456789.09

If you declare as above then the easiest way will be by importing
import java.text.NumberFormat;
import java.util.Locale;

 System.out.println(NumberFormat.getNumberInstance(Locale.US).format(x)); System.out.println(NumberFormat.getNumberInstance(Locale.US).format(y));

If you are keen to use only String then the below code will help
          String  x= "123456789";
         String y= "123456789.09" ;
         double xtodub = Double.parseDouble(x);
        double ytodub = Double.parseDouble(y);
It did not work,...

Your code is giving   output  123,456,789

Whereas I asked output as    123456,789

What is the fix ?

I am not sure of your exact requirement.

For the first input and output you posted the below code will work if those values are of that same lengths.

String xformatted = x.substring(0,6) + "," + x.substring(6,9)
String yformatted = y.substring(0,6) + "," + y.substring(6,12)

If your x and y length of digits  change always then  you need to get their lengths.
If your x value will also have some time decimals then you need to search for the . (point) and if the decimal is there then you need the v value to be 5 instead of 3 by using a if loop.

  String x = "123456789";
        String y = "123456789.09";
        int l = 0;
        int v = 0;
        l = x.length();
        v = l-3;
        String xformatted = x.substring(0, v) + "," + x.substring(v, 9);
        String yformatted = y.substring(0, v) + "," + y.substring(v, 12);
Why are you holding a number as a String?
Avatar of mccarl
I don't think you can get the NumberFormat classes to only do thousands separators (and not millions, etc). So easiest bet might be just to fiddle with Strings, as much as you should try not to do that...

    public void testStringNumbers(){

    private String format(String num) {
        if (num.indexOf(',') >= 0) {
            return num;
        int decPos = num.lastIndexOf('.');
        if (decPos == -1) {
            decPos = num.length();
        if (decPos > 3) {
            return num.substring(0, decPos - 3) + "," + num.substring(decPos - 3);
        return num;

Open in new window


Try with "contains" and you will always get a comma on the place you are looking for even if the string lengths change.
If you have more than x and y values then put that code into a function and return v and len values for all your strings with numbers.
        String x = "123456789";
        String y = "123456789.09";
        int xlen = 0;
        int ylen = 0 ;
        int v = 0;
        xlen = x.length();
        ylen = y.length();
        boolean decx =x.contains(".");
        boolean decy = y.contains(".");
        if (decx)  v = xlen-5 ;
        else   v = xlen-3;
        String xformatted = x.substring(0, v) + "," + x.substring(v, xlen);
        v = 0;
        if (decy) v = ylen - 5 ;
                else v = ylen - 3;
        String yformatted = y.substring(0, v) + "," + y.substring(v, ylen);
        System.out.println("xformatted = " + xformatted);
        System.out.println("yformatted = " + yformatted);

Open in new window

Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Similar to Jim's first solution using StringBuilder in place of StringUtils -
public static String addComma(String str) {
            StringBuilder sb = new StringBuilder(str);
            boolean hasDot = false;
            if (sb.indexOf(".") >= 0) {hasDot = true;}
            return hasDot && sb.indexOf(".") > 3 ? sb.insert(sb.indexOf(".") - 3, ",").toString() : !hasDot && sb.length() > 3 ? sb.insert(sb.length() - 3, ",").toString() : sb.toString();