c language floating-point data to extract an integer portion and the fractional part of several methods

A former record

 

  Recent data related to the various sensors and transmitted through the wifi bt out, which relates to the problem of floating-point transmission, in order to facilitate transport, author's approach generally is to separate the integer and fractional parts, namely transmission, which is relatively simple clarity a.

 

Two methodological

   In fact, the floating point disassembled into two parts, there are many methods, and here I polished off two three commonly used to make a summary to facilitate follow-up of friends and myself to use it.

  A subtraction:

#include <stdio.h>

int main()
{
    
    double test = -12.1234;

    int a_int = (int)test;

    int b_int = (test - a_int)*10000;

    printf("a_int:%d b_int:%d ",a_int,b_int);


    return 0;
}

   

Function Method B:

 

#include <stdio.h> 
#include <math.h>
 int main () {
     Double X = 3.1415 , intpart;   // given initial variable 
    Double fractpart MODF = (X, & intpart);   // find the fractional part 3.1415 
    the printf ( " intpart:% LF \ nfractpart:% LF \ n- " , intpart, fractpart);
     return  0 ; 
}

 

III summarizes

   

This implementation, there are many, here are only the most simple two kinds of it, I believe, Road to SR, no matter what products and solutions are more simple and more display level ah. Finally, send word:

Sea world Could Wang soil, the rate of soil, WANG Chen. The position of their own things to do trying to be difficult.

 

Guess you like

Origin www.cnblogs.com/dylancao/p/12641581.html