Topic: 如何将整型值转为字符串

  Print this page

1.如何将整型值转为字符串 Copy to clipboard
Posted by: programfanny
Posted on: 2007-06-13 16:46

如何将一个整型值转为字符串? 如:
{
String[] id=new String[10];
int i;
for (i=1 ;i<10;i++)
{
id[i]=i; // <------这里该怎样写?
}
}

2.Re:如何将整型值转为字符串 [Re: programfanny] Copy to clipboard
Posted by: hollyman
Posted on: 2007-06-13 17:35

id[i]=Integer.toString(i);

3.Re:如何将整型值转为字符串 [Re: programfanny] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2007-06-13 18:39

public String f(int i)
  {
    if(i == 0)
      return "0";
    
    StringBuffer buffer = new StringBuffer();
    int sign = i /Math.abs(i);
    String s = sign >= 0 ? "" : "-";
    
    i = i * sign;
    
    while(i >= 1)
    {
      buffer.append(i % 10);
      i = i / 10;
    }
    
    buffer.append(s);
    return buffer.reverse().toString();
  }

看看我写的代码,希望对你有帮助

4.Re:如何将整型值转为字符串 [Re: programfanny] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2007-06-13 18:43

斑竹记得给我加分啊


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923