1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Remove some decimal

Discussion in 'C' started by david_BS, Apr 9, 2012.

  1. david_BS

    david_BS New Member

    Joined:
    Apr 5, 2012
    Messages:
    16
    Likes Received:
    3
    Trophy Points:
    0
    Hi, this function can remove some decimals from your floating point number. I just created in 1 minute just in response to some person's question in another community

    Code:
    #include<iostream.h>
    #include<stdio.h>
    
    double RemDecimals(double dNum, unsigned int iCant){
    
    	int inum = (int)dNum;
    	unsigned int unidades =1;
    	double resnum=dNum;
    	if(inum>9){
    		unidades++;//porque es de dos cifras ya
    		while((inum/10)>9){//si es mayor a 99
    			unidades++;
    			inum/=10;
    		}
    		for(unsigned int i=0;i< iCant;i++) dNum*=10;
    		//int cifras = unidades+iCant;
    		int inum2 = (int)dNum;
    		double newnum = (float)inum2;
    		for(unsigned int j=0;j< iCant;j++) newnum/=10;
    		resnum = (float)newnum;
    	}
    
    	return resnum;
    }
    
    int main(){
    
    	double num = 93.161599;
    
    	printf("num before: %f\n",num);
    
    	num = RemDecimals(num,2);
    
    	printf("num after: %f\n",num);
    
    	cin.get();
    	return 0;
    
    }
     
  2. state

    state New Member

    Joined:
    Sep 14, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Well Done
     

Share This Page