Remove some decimal

david_BS's Avatar, Join Date: Apr 2012
Go4Expert Member
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;

}
0
state's Avatar, Join Date: Sep 2011
Go4Expert Member
Well Done