Float.toString() 和 Integer.toString() 如何工作?

发布于 2024-11-06 23:52:32 字数 346 浏览 6 评论 0原文

我如何实现将 float 或 int 转换为字符串的算法? 我找到了一个链接 http:// /geeksforgeeks.org/forum/topic/amazon-interview-question-for-software-engineerdeveloper-0-2-years-about-algorithms-13

但我无法理解那里给出的算法

How can i implement an algorithm to convert float or int to string?
I found one link
http://geeksforgeeks.org/forum/topic/amazon-interview-question-for-software-engineerdeveloper-0-2-years-about-algorithms-13

but i cant understand the algorithm given there

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半步萧音过轻尘 2024-11-13 23:52:32

数字 0-9 在大多数字符编码中都是连续的,因此调整它的整数值在这里会有所帮助:

int val;
String str="";
while(val>0){
    str = ('0'+(val%10)) + str;
    val /= 10;
}

the numbers 0-9 are sequential in most character encoding so twiddling with the integral value of it will help here:

int val;
String str="";
while(val>0){
    str = ('0'+(val%10)) + str;
    val /= 10;
}
心的位置 2024-11-13 23:52:32

这是如何将整数转换为字符串的示例,我希望您能够从中找出如何将浮点数转换为字符串。

public String intToString(int value) {
  StringBuffer buffer = new StringBuffer();
  if (value < 0) {
    buffer.append("-");
  }
  // MAX_INT is just over 2 billion, so start by finding the number of billions.
  int divisor = 1000000000;
  while (divisor > 0) {
    int digit = value / divisor;  // integer division, so no remainder.
    if (digit > 0) {
      buffer.append('0'+digit);
      value = value - digit * divisor; // subtract off the value to zero out that digit.
    }
    divisor = divisor / 10; // the next loop iteration should be in the 10's place to the right
  }
}

当然,这是非常未优化的,但它让您了解最基本的格式化是如何完成的。

请注意,"" + x 的技术实际上被重写为类似的内容,

StringBuffer buffer = new StringBuffer();
buffer.append("");
buffer.append(String.valueOf(x));
buffer.toString();

所以不要认为所写的内容 100% 完全是如何完成的,看看是在一个对事物有更大的看法。

Here's a sample of how to do the integer to string, from it I hope you'll be able to figure out how to do the float to string.

public String intToString(int value) {
  StringBuffer buffer = new StringBuffer();
  if (value < 0) {
    buffer.append("-");
  }
  // MAX_INT is just over 2 billion, so start by finding the number of billions.
  int divisor = 1000000000;
  while (divisor > 0) {
    int digit = value / divisor;  // integer division, so no remainder.
    if (digit > 0) {
      buffer.append('0'+digit);
      value = value - digit * divisor; // subtract off the value to zero out that digit.
    }
    divisor = divisor / 10; // the next loop iteration should be in the 10's place to the right
  }
}

This is of course, very unoptimized, but it gives you a feel for how the most basic formatting is accomplished.

Note that the technique of "" + x is actually rewritten to be something like

StringBuffer buffer = new StringBuffer();
buffer.append("");
buffer.append(String.valueOf(x));
buffer.toString();

So don't think that what is written is 100% exactly HOW it is done, look at is as what must happen in a larger view of things.

迷你仙 2024-11-13 23:52:32

总体思路是通过余数十来选取最低有效数字。然后将该数字除以 10 并重复...直到剩下零。

当然,它比这更复杂一点,特别是在 float 情况下。


如果 int fomrat 中有一个数字,那么我需要将其插入到 char 中,如何将 int 转换为 char?

简单的:

int digit = ... /* 0 to 9 */
char ch = (char)('0' + digit);

The general idea is to pick off the least significant digit by taking the number remainder ten. Then divide the number by 10 and repeat ... until you are left with zero.

Of course, it is a bit more complicated than that, especially in the float case.


if i have a single digit in int fomrat then i need to insert it into char , how to convert int to char?

Easy:

int digit = ... /* 0 to 9 */
char ch = (char)('0' + digit);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文