Java - Convert double to int array
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
I have a double
double pi = 3.1415;
I want to convert this to a int array
int piArray = 3,1,4,1,5;
I came up with this
double pi = 3.1415;
String piString = Double.toString(pi).replace(".", "");
int piArray = new int[piString.length()];
for (int i = 0; i <= piString.length()-1; i++)
piArray[i] = piString.charAt(i) - '0';
It's working but I don't like this solution because I think a lot of conversions between datatypes can lead to errors. Is my code even complete or do I need to check for something else?
And how would you approach this problem? Wasn't able to find a similar problem here on stackoverflow for Java.
java arrays type-conversion int double
add a comment |Â
up vote
10
down vote
favorite
I have a double
double pi = 3.1415;
I want to convert this to a int array
int piArray = 3,1,4,1,5;
I came up with this
double pi = 3.1415;
String piString = Double.toString(pi).replace(".", "");
int piArray = new int[piString.length()];
for (int i = 0; i <= piString.length()-1; i++)
piArray[i] = piString.charAt(i) - '0';
It's working but I don't like this solution because I think a lot of conversions between datatypes can lead to errors. Is my code even complete or do I need to check for something else?
And how would you approach this problem? Wasn't able to find a similar problem here on stackoverflow for Java.
java arrays type-conversion int double
You might be interested in base 2 digits, a double as array of 0s and 1s. The Double class has conversion of the bits. Above you could have the power-of-ten in the array: -4 in your example. Doubles are themselves problematic, as the toString could well give "3.14149999998"
– Joop Eggen
25 mins ago
add a comment |Â
up vote
10
down vote
favorite
up vote
10
down vote
favorite
I have a double
double pi = 3.1415;
I want to convert this to a int array
int piArray = 3,1,4,1,5;
I came up with this
double pi = 3.1415;
String piString = Double.toString(pi).replace(".", "");
int piArray = new int[piString.length()];
for (int i = 0; i <= piString.length()-1; i++)
piArray[i] = piString.charAt(i) - '0';
It's working but I don't like this solution because I think a lot of conversions between datatypes can lead to errors. Is my code even complete or do I need to check for something else?
And how would you approach this problem? Wasn't able to find a similar problem here on stackoverflow for Java.
java arrays type-conversion int double
I have a double
double pi = 3.1415;
I want to convert this to a int array
int piArray = 3,1,4,1,5;
I came up with this
double pi = 3.1415;
String piString = Double.toString(pi).replace(".", "");
int piArray = new int[piString.length()];
for (int i = 0; i <= piString.length()-1; i++)
piArray[i] = piString.charAt(i) - '0';
It's working but I don't like this solution because I think a lot of conversions between datatypes can lead to errors. Is my code even complete or do I need to check for something else?
And how would you approach this problem? Wasn't able to find a similar problem here on stackoverflow for Java.
java arrays type-conversion int double
java arrays type-conversion int double
asked 35 mins ago
Ian Fako
282112
282112
You might be interested in base 2 digits, a double as array of 0s and 1s. The Double class has conversion of the bits. Above you could have the power-of-ten in the array: -4 in your example. Doubles are themselves problematic, as the toString could well give "3.14149999998"
– Joop Eggen
25 mins ago
add a comment |Â
You might be interested in base 2 digits, a double as array of 0s and 1s. The Double class has conversion of the bits. Above you could have the power-of-ten in the array: -4 in your example. Doubles are themselves problematic, as the toString could well give "3.14149999998"
– Joop Eggen
25 mins ago
You might be interested in base 2 digits, a double as array of 0s and 1s. The Double class has conversion of the bits. Above you could have the power-of-ten in the array: -4 in your example. Doubles are themselves problematic, as the toString could well give "3.14149999998"
– Joop Eggen
25 mins ago
You might be interested in base 2 digits, a double as array of 0s and 1s. The Double class has conversion of the bits. Above you could have the power-of-ten in the array: -4 in your example. Doubles are themselves problematic, as the toString could well give "3.14149999998"
– Joop Eggen
25 mins ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
6
down vote
I don't know stright way but I think It is simpler:
int piArray = String.valueOf(pi).replaceAll("\D", "").chars().map(Character::getNumericValue).toArray();
1
Came up with the same solution, you were a bit earlier :)
– Glains
27 mins ago
4
I have usedreplaceAll("\D", "")
tough to filter any non digits.
– Glains
25 mins ago
Glains post your solution or edit, as i think it's the more robust solution.
– fl0w
23 mins ago
Thanks @Glains, post edited
– Rahim Dastar
20 mins ago
@RahimDastar since adouble
has a single.
, usereplaceFirst
instead ofreplaceAll
, otherwise 1+
– Eugene
11 mins ago
 |Â
show 3 more comments
up vote
1
down vote
int result = Stream.of(pi)
.map(String::valueOf)
.flatMap(x -> Arrays.stream(x.split("\.|")))
.filter(x -> !x.isEmpty())
.mapToInt(Integer::valueOf)
.toArray();
Or a safer approach with java-9
:
int result = new Scanner(String.valueOf(pi))
.findAll(Pattern.compile("\d"))
.map(MatchResult::group)
.mapToInt(Integer::valueOf)
.toArray();
Can you explain why it is safer, would be interested in that.
– Glains
59 secs ago
@Glains safer that my first approach, which does not take care of1.13E+21
...
– Eugene
23 secs ago
add a comment |Â
up vote
0
down vote
You can use in java 8
int piArray = piString.chars().map(Character::getNumericValue).toArray();
add a comment |Â
up vote
0
down vote
this would work also
int piArray = Double.toString(pi).replace(".", "").chars().map(c -> c-'0').toArray();
add a comment |Â
up vote
-1
down vote
public class Main
public static void main(String args)
Double pi = 3.14159;
char chars = pi.toString().toCharArray();
System.out.println(chars.length);
Use char array to handle it.
The decimal point, handle it yourself.
New contributor
CHONG CHEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
I don't know stright way but I think It is simpler:
int piArray = String.valueOf(pi).replaceAll("\D", "").chars().map(Character::getNumericValue).toArray();
1
Came up with the same solution, you were a bit earlier :)
– Glains
27 mins ago
4
I have usedreplaceAll("\D", "")
tough to filter any non digits.
– Glains
25 mins ago
Glains post your solution or edit, as i think it's the more robust solution.
– fl0w
23 mins ago
Thanks @Glains, post edited
– Rahim Dastar
20 mins ago
@RahimDastar since adouble
has a single.
, usereplaceFirst
instead ofreplaceAll
, otherwise 1+
– Eugene
11 mins ago
 |Â
show 3 more comments
up vote
6
down vote
I don't know stright way but I think It is simpler:
int piArray = String.valueOf(pi).replaceAll("\D", "").chars().map(Character::getNumericValue).toArray();
1
Came up with the same solution, you were a bit earlier :)
– Glains
27 mins ago
4
I have usedreplaceAll("\D", "")
tough to filter any non digits.
– Glains
25 mins ago
Glains post your solution or edit, as i think it's the more robust solution.
– fl0w
23 mins ago
Thanks @Glains, post edited
– Rahim Dastar
20 mins ago
@RahimDastar since adouble
has a single.
, usereplaceFirst
instead ofreplaceAll
, otherwise 1+
– Eugene
11 mins ago
 |Â
show 3 more comments
up vote
6
down vote
up vote
6
down vote
I don't know stright way but I think It is simpler:
int piArray = String.valueOf(pi).replaceAll("\D", "").chars().map(Character::getNumericValue).toArray();
I don't know stright way but I think It is simpler:
int piArray = String.valueOf(pi).replaceAll("\D", "").chars().map(Character::getNumericValue).toArray();
edited 20 mins ago
answered 28 mins ago
Rahim Dastar
419110
419110
1
Came up with the same solution, you were a bit earlier :)
– Glains
27 mins ago
4
I have usedreplaceAll("\D", "")
tough to filter any non digits.
– Glains
25 mins ago
Glains post your solution or edit, as i think it's the more robust solution.
– fl0w
23 mins ago
Thanks @Glains, post edited
– Rahim Dastar
20 mins ago
@RahimDastar since adouble
has a single.
, usereplaceFirst
instead ofreplaceAll
, otherwise 1+
– Eugene
11 mins ago
 |Â
show 3 more comments
1
Came up with the same solution, you were a bit earlier :)
– Glains
27 mins ago
4
I have usedreplaceAll("\D", "")
tough to filter any non digits.
– Glains
25 mins ago
Glains post your solution or edit, as i think it's the more robust solution.
– fl0w
23 mins ago
Thanks @Glains, post edited
– Rahim Dastar
20 mins ago
@RahimDastar since adouble
has a single.
, usereplaceFirst
instead ofreplaceAll
, otherwise 1+
– Eugene
11 mins ago
1
1
Came up with the same solution, you were a bit earlier :)
– Glains
27 mins ago
Came up with the same solution, you were a bit earlier :)
– Glains
27 mins ago
4
4
I have used
replaceAll("\D", "")
tough to filter any non digits.– Glains
25 mins ago
I have used
replaceAll("\D", "")
tough to filter any non digits.– Glains
25 mins ago
Glains post your solution or edit, as i think it's the more robust solution.
– fl0w
23 mins ago
Glains post your solution or edit, as i think it's the more robust solution.
– fl0w
23 mins ago
Thanks @Glains, post edited
– Rahim Dastar
20 mins ago
Thanks @Glains, post edited
– Rahim Dastar
20 mins ago
@RahimDastar since a
double
has a single .
, use replaceFirst
instead of replaceAll
, otherwise 1+– Eugene
11 mins ago
@RahimDastar since a
double
has a single .
, use replaceFirst
instead of replaceAll
, otherwise 1+– Eugene
11 mins ago
 |Â
show 3 more comments
up vote
1
down vote
int result = Stream.of(pi)
.map(String::valueOf)
.flatMap(x -> Arrays.stream(x.split("\.|")))
.filter(x -> !x.isEmpty())
.mapToInt(Integer::valueOf)
.toArray();
Or a safer approach with java-9
:
int result = new Scanner(String.valueOf(pi))
.findAll(Pattern.compile("\d"))
.map(MatchResult::group)
.mapToInt(Integer::valueOf)
.toArray();
Can you explain why it is safer, would be interested in that.
– Glains
59 secs ago
@Glains safer that my first approach, which does not take care of1.13E+21
...
– Eugene
23 secs ago
add a comment |Â
up vote
1
down vote
int result = Stream.of(pi)
.map(String::valueOf)
.flatMap(x -> Arrays.stream(x.split("\.|")))
.filter(x -> !x.isEmpty())
.mapToInt(Integer::valueOf)
.toArray();
Or a safer approach with java-9
:
int result = new Scanner(String.valueOf(pi))
.findAll(Pattern.compile("\d"))
.map(MatchResult::group)
.mapToInt(Integer::valueOf)
.toArray();
Can you explain why it is safer, would be interested in that.
– Glains
59 secs ago
@Glains safer that my first approach, which does not take care of1.13E+21
...
– Eugene
23 secs ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
int result = Stream.of(pi)
.map(String::valueOf)
.flatMap(x -> Arrays.stream(x.split("\.|")))
.filter(x -> !x.isEmpty())
.mapToInt(Integer::valueOf)
.toArray();
Or a safer approach with java-9
:
int result = new Scanner(String.valueOf(pi))
.findAll(Pattern.compile("\d"))
.map(MatchResult::group)
.mapToInt(Integer::valueOf)
.toArray();
int result = Stream.of(pi)
.map(String::valueOf)
.flatMap(x -> Arrays.stream(x.split("\.|")))
.filter(x -> !x.isEmpty())
.mapToInt(Integer::valueOf)
.toArray();
Or a safer approach with java-9
:
int result = new Scanner(String.valueOf(pi))
.findAll(Pattern.compile("\d"))
.map(MatchResult::group)
.mapToInt(Integer::valueOf)
.toArray();
edited 3 mins ago
answered 26 mins ago


Eugene
59.8k982141
59.8k982141
Can you explain why it is safer, would be interested in that.
– Glains
59 secs ago
@Glains safer that my first approach, which does not take care of1.13E+21
...
– Eugene
23 secs ago
add a comment |Â
Can you explain why it is safer, would be interested in that.
– Glains
59 secs ago
@Glains safer that my first approach, which does not take care of1.13E+21
...
– Eugene
23 secs ago
Can you explain why it is safer, would be interested in that.
– Glains
59 secs ago
Can you explain why it is safer, would be interested in that.
– Glains
59 secs ago
@Glains safer that my first approach, which does not take care of
1.13E+21
...– Eugene
23 secs ago
@Glains safer that my first approach, which does not take care of
1.13E+21
...– Eugene
23 secs ago
add a comment |Â
up vote
0
down vote
You can use in java 8
int piArray = piString.chars().map(Character::getNumericValue).toArray();
add a comment |Â
up vote
0
down vote
You can use in java 8
int piArray = piString.chars().map(Character::getNumericValue).toArray();
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can use in java 8
int piArray = piString.chars().map(Character::getNumericValue).toArray();
You can use in java 8
int piArray = piString.chars().map(Character::getNumericValue).toArray();
answered 18 mins ago
Sveteek
215
215
add a comment |Â
add a comment |Â
up vote
0
down vote
this would work also
int piArray = Double.toString(pi).replace(".", "").chars().map(c -> c-'0').toArray();
add a comment |Â
up vote
0
down vote
this would work also
int piArray = Double.toString(pi).replace(".", "").chars().map(c -> c-'0').toArray();
add a comment |Â
up vote
0
down vote
up vote
0
down vote
this would work also
int piArray = Double.toString(pi).replace(".", "").chars().map(c -> c-'0').toArray();
this would work also
int piArray = Double.toString(pi).replace(".", "").chars().map(c -> c-'0').toArray();
answered 3 mins ago
The Scientific Method
568310
568310
add a comment |Â
add a comment |Â
up vote
-1
down vote
public class Main
public static void main(String args)
Double pi = 3.14159;
char chars = pi.toString().toCharArray();
System.out.println(chars.length);
Use char array to handle it.
The decimal point, handle it yourself.
New contributor
CHONG CHEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
-1
down vote
public class Main
public static void main(String args)
Double pi = 3.14159;
char chars = pi.toString().toCharArray();
System.out.println(chars.length);
Use char array to handle it.
The decimal point, handle it yourself.
New contributor
CHONG CHEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
public class Main
public static void main(String args)
Double pi = 3.14159;
char chars = pi.toString().toCharArray();
System.out.println(chars.length);
Use char array to handle it.
The decimal point, handle it yourself.
New contributor
CHONG CHEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
public class Main
public static void main(String args)
Double pi = 3.14159;
char chars = pi.toString().toCharArray();
System.out.println(chars.length);
Use char array to handle it.
The decimal point, handle it yourself.
New contributor
CHONG CHEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
CHONG CHEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 18 mins ago


CHONG CHEN
11
11
New contributor
CHONG CHEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
CHONG CHEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
CHONG CHEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52328329%2fjava-convert-double-to-int-array%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You might be interested in base 2 digits, a double as array of 0s and 1s. The Double class has conversion of the bits. Above you could have the power-of-ten in the array: -4 in your example. Doubles are themselves problematic, as the toString could well give "3.14149999998"
– Joop Eggen
25 mins ago