VB icon

Int To String

Email
Submitted on: 1/6/2015 4:56:00 PM
By: Morti (from psc cd)  
Level: Advanced
User Rating: By 7 Users
Compatibility: C++ (general)
Views: 391
 
     Upss.. I'm really sorry I changed the Title, it was String to Int, but my function is to convert an Int Value to a String Value *sorry* :) Well, there are two functions I'll post, I think they are very helpfull and I haven't found much informations about it. //The first an main Function is a Function to Convert an Integer to String. But for this I also needed a function to get the length of an Integer, so I also wrote an extra function for this.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :IntTo String
//**************************************
#include <string>
#include <stdlib.h>
using namespace std;
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: IntTo String
// Description:Upss.. I'm really sorry I changed the Title, it was String to Int, but my function is to convert an Int Value to a String Value *sorry* :)
Well, there are two functions I'll post, I think they are very helpfull and I haven't found much informations about it.
//The first an main Function is a Function to Convert an Integer to String. But for this I also needed a function to get the length of an Integer, so I also wrote an extra function for this.
// By: Morti (from psc cd)
//
// Inputs://Input for IntToString = Integer Value
//Input for IntLen = Integer Value
//
// Returns://Return for IntToString = String Value
//Return for IntLen = Integer Value
//**************************************

//Function to return the Length of an Integer
int IntLen(int value){
 int div=1;
 int len=1;
 do{
 div*=10;
 if ((int) ( (double)value / (double)(div)) ==0){
 return len;
 } 
 len +=1;
 }while(true);
}
//Function to Convert an Integer to String
string IntToString(int value){
 string number;
 int temp;
 bool neg= false;
 //Proof if the number has a negiote sign
 if ((value * (-1)) > value){
 value *=-1;
 neg=true;
 }
 for (int z = IntLen(value) ;z>=1;z--){
 //Modulo to get the last number 
 temp = value % 10;
 //add Number to the String
 number = ((char)(temp + 48))+ number;
 //Substract the last number from the Ingeter
 value = (value - temp) /10;
 temp *= 10 ;
 }
 //Set the sign again
 if (neg==true){ number="-" + number; }
 return number;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.