Reputation: 13
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?
Upvotes: 1
Views: 124
Reputation: 19847
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
Upvotes: 0
Reputation: 12943
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.
Upvotes: 0
Reputation: 14199
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);
Upvotes: 1