Home Ask Login Register

Developers Planet

Your answer is one click away!

Thomas February 2016

Replacing substring

public class test
    public static void main(String[] args)
        String abra = "100000";
        abra = abra.replace(abra.substring(abra.length() - 3),
               "," + abra.substring(abra.length() - 3));

Totally new to java and i'm trying to replace a number with comma to separate the zeros by three. It works fine for thousands and ten thousands, but i'm getting wierd results for more. Any ideas?

Thanks in advance!


Maroun Maroun February 2016

Because you're manually only for 6 digits. A better approach will be using NumberFormat#getNumberInstance with Locale.US:

A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user. For example, displaying a number is a locale-sensitive operation— the number should be formatted according to the customs and conventions of the user's native country, region, or culture.

// 20,000,000

Post Status

Asked in February 2016
Viewed 1,131 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life