Converting float to int (was: Re: [Dev-C++] (no subject) )
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Tim S. <ti...@we...> - 2004-01-01 10:57:03
|
Hi 'neo', you have written: > Question: How to convert the a double date into a string,without using > some certain function of the library? If you would use C++ this would be very easy with use of the stringstream class. Writing your own function is not hard but needs a bit time. You have to split your double into the float part and an integer part. double flnumber = 233.141592; int integerpart = (int) pi; double floatpart = ((int)pi) - pi; std::string number; Now you must parse your integerpart with something like this: while(integerpart <= 0) { number.append(int2char(integerpart - ((integerpart / 10)* 10)); integepart /= 10; } char int2char(int digit) { switch(digit) { case 0: return '0'; break; ... default: return '\0'; break; } } Now we are comming to the floatpart: number.append(","); while (floatpart <= 0) { number.append(int2char((int)(floatpart * 10))); floatpart *= 10; } No guarantee that it works, but I think you get the idea how it should be done. so long Tim -- http://we-are-teh-b.org/~tim/borg.key |