Java - Convert double to int array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
10
down vote

favorite
2












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.










share|improve this question





















  • 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














up vote
10
down vote

favorite
2












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.










share|improve this question





















  • 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












up vote
10
down vote

favorite
2









up vote
10
down vote

favorite
2






2





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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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();





share|improve this answer


















  • 1




    Came up with the same solution, you were a bit earlier :)
    – Glains
    27 mins ago






  • 4




    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










  • 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

















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();





share|improve this answer






















  • 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

















up vote
0
down vote













You can use in java 8



int piArray = piString.chars().map(Character::getNumericValue).toArray();





share|improve this answer



























    up vote
    0
    down vote













    this would work also



    int piArray = Double.toString(pi).replace(".", "").chars().map(c -> c-'0').toArray();




    share



























      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.






      share|improve this answer








      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.

















        Your Answer





        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        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






























        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();





        share|improve this answer


















        • 1




          Came up with the same solution, you were a bit earlier :)
          – Glains
          27 mins ago






        • 4




          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










        • 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














        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();





        share|improve this answer


















        • 1




          Came up with the same solution, you were a bit earlier :)
          – Glains
          27 mins ago






        • 4




          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










        • 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












        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();





        share|improve this answer














        I don't know stright way but I think It is simpler:



        int piArray = String.valueOf(pi).replaceAll("\D", "").chars().map(Character::getNumericValue).toArray();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        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 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










        • 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












        • 1




          Came up with the same solution, you were a bit earlier :)
          – Glains
          27 mins ago






        • 4




          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










        • 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







        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












        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();





        share|improve this answer






















        • 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














        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();





        share|improve this answer






















        • 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












        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();





        share|improve this answer














        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();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        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 of 1.13E+21...
          – Eugene
          23 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















        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










        up vote
        0
        down vote













        You can use in java 8



        int piArray = piString.chars().map(Character::getNumericValue).toArray();





        share|improve this answer
























          up vote
          0
          down vote













          You can use in java 8



          int piArray = piString.chars().map(Character::getNumericValue).toArray();





          share|improve this answer






















            up vote
            0
            down vote










            up vote
            0
            down vote









            You can use in java 8



            int piArray = piString.chars().map(Character::getNumericValue).toArray();





            share|improve this answer












            You can use in java 8



            int piArray = piString.chars().map(Character::getNumericValue).toArray();






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 18 mins ago









            Sveteek

            215




            215




















                up vote
                0
                down vote













                this would work also



                int piArray = Double.toString(pi).replace(".", "").chars().map(c -> c-'0').toArray();




                share
























                  up vote
                  0
                  down vote













                  this would work also



                  int piArray = Double.toString(pi).replace(".", "").chars().map(c -> c-'0').toArray();




                  share






















                    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();




                    share












                    this would work also



                    int piArray = Double.toString(pi).replace(".", "").chars().map(c -> c-'0').toArray();





                    share











                    share


                    share










                    answered 3 mins ago









                    The Scientific Method

                    568310




                    568310




















                        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.






                        share|improve this answer








                        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.





















                          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.






                          share|improve this answer








                          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.



















                            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.






                            share|improve this answer








                            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.







                            share|improve this answer








                            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.









                            share|improve this answer



                            share|improve this answer






                            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.



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                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













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                Confectionery