How do Float.toString () and Integer.toString () work?

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 can not understand the algorithm given there

0
source share
4 answers

the numbers 0-9 are consecutive in most character encodings, so merging with the integral value will help here:

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

Here is an example of how to make an integer into a string, from it I hope you can figure out how to make a float for a 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 place to the right
  }
}

, , , , .

, "" + x

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

, 100% , , .

+3

, .

+2

, , . 10 ... .

, , float.


int fomrat, char, int char?

:

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

All Articles