1

Can I get the number after the comma and put into a variable, and only two before the comma and put into another variable on Android?

I'm trying to do something like that...

I have the number 12,3456789 I want to get "12" and put into a variable A. Then I want to get "34" and put into a variable B, there is a way to do that?

2
  • So you want all the numbers in the left of comma and only 2 to the right of comma? Commented Jul 11, 2013 at 19:29
  • Yes, only two to the right. Commented Jul 11, 2013 at 19:34

3 Answers 3

1

Try this:

        String all="12,3456789";
        String[] temp=all.split(",");
        String a=temp[0]; //12
        String b=temp[1]; //3456789

Edit:

if you want to get 2 no's after , than use b.substring(0, 2)

like :

  String c=b.substring(0, 2);
Sign up to request clarification or add additional context in comments.

Comments

0

Funny stuff:

double x = 12,3456789
List<Integer> values = new ArrayList<Integer>;

while( x != 0) {

    int y = x; // 12
    values.add(y);

    x = (x - y) * 100; // 34,56789

}

To save it to an array you have to know how many digits x has to know how much elements you have to put in. I don't know a proper way right know so I'm using a Collection here.

Comments

0

What Tarsem presented is ok, but you can also parse that String as a double and later do with it what you want:

import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args)
    {
        String s = "12.3456789";
        double d = new Double(s);
        int a = (int) d;
        int b = (int) ((d - a) * 100);
        System.out.println(a);
        System.out.println(b);
    }
}

Output:

12
34

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.