Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Remove some decimal (http://www.go4expert.com/forums/remove-some-decimal-t28159/)

david_BS 10Apr2012 04:20

Remove some decimal
 
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;

}


state 19Apr2012 11:44

Re: Remove some decimal
 
Well Done


All times are GMT +5.5. The time now is 05:28.