1

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

ako
  • 2,511
  • 3
  • 18
  • 11
  • 1
    Are you asking how one might implement it, or how the JDK actually implements it? – Dilum Ranatunga May 12 '11 at 14:40
  • its a interview question, i wont to implemt my own algo – ako May 12 '11 at 14:44
  • integer to string is more simple. for double to string this guidlines might be helpful: [javaDocs](http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html#toString(double)) – zviadm May 25 '11 at 19:27

4 Answers4

8

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;
}
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
3

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.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • In the event that someone tries to swap out character sets with one that doesn't have it's digits sequentially (remember this is an interview question, not a real world problem) you can always implement the `buffer.append('0'+digit)` with a switch statement, like so `switch (digit) { case 0 : buffer.append("0"); break; case 1 : buffer.append("1"); break; ... }` – Edwin Buck May 12 '11 at 14:58
  • Using StringBuffer is totally useless as the java compiler will replace string concatenation with one anyway. – Nicolas Bousquet May 12 '11 at 15:36
  • 1
    @Nicolas, The compiler only replaces String concatenation with a `StringBuffer` in the event that all of the concatenations occur within the same localized block. So, `System.out.println("(" + x + ", " + y + ")");` works just fine but, `String x = ""; for (int i = 0; i < 5; i++) { x = x + '0'+i; }` won't maintain a buffer between loop iterations. – Edwin Buck May 12 '11 at 19:34
2

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);
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    agreed, but if i have a single digit in int fomrat then i need to insert it into char , how to convert int to char? – ako May 12 '11 at 14:46
2

Well, you can read the code yourself.

Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71