ricardo spek ricardo spek - 1 year ago 113
Java Question

Add a dot inbetween a string or int to create a double JAVA

I need something to turn somthing like this: 29939299322 into 299392.99322 automatically as it has to do this alot.
i have found things like format but it only seems to be able to add things in front of the number but this has to happen after the sixth number.

preferably from an int directly into a double. but i haven't even found a way t do it from a string.

so if any of u know if this is even possible please help me do this.
and it has to be java. if u also know any documentation which can help me also post it,

thanks in advance

Answer Source

Well, if you are using Strings, you could use regex (Not really efficient but clean and readable:P) :

public static void main(String[] args) {
    String s1 = "29939299322";
    System.out.println(s1.replaceAll("(\\d{6})(\\d+)", "$1.$2"));

O/P :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download