How to convert an integer value to string?

前端 未结 5 1748
遥遥无期
遥遥无期 2021-02-14 06:26

How do I convert an integer variable to a string variable in Java?

5条回答
  •  爱一瞬间的悲伤
    2021-02-14 07:09

    Here is the method manually convert the int to String value.Anyone correct me if i did wrong.

    /**
     * @param a
     * @return
     */
    private String convertToString(int a) {
    
        int c;
        char m;
        StringBuilder ans = new StringBuilder();
        // convert the String to int
        while (a > 0) {
            c = a % 10;
            a = a / 10;
            m = (char) ('0' + c);
            ans.append(m);
        }
        return ans.reverse().toString();
    }
    

提交回复
热议问题