Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Truncate a float number with a desired decimal limit

I would like to truncate a float number 1234. 056789 back to float number but with a desired fraction as the following:
 
1.  numb = truncate(1234. 056789, 3)  ---> numb = 1234.056
2.  numb = truncate(1234. 056789, 5)  ---> numb = 1234.05678
 
I did try:
 
float math_truncate(float numb, int n_trunc)
{
      int   i, n = pow(10,n_trunc);
      double num;
 
      i = (int)(numb * n); 
  
      num = i/(n * 1.0); 
 
     return (float)num;
}
 
Before return I have:
 
Case #1: num = 1234.056000000 ... after return, I have 1234.056107
Case #2: num = 1234.056780000 ... after return, I have 1234.05678107
 
Note1: I did try other methods, but the results are not as #1 & #2 ... always get some after the desired decimal point
Note2: The TruncateRealNumber (number) only truncate all fraction part ... num = 1234.00000)
 
*) Anyone can help? Smiley Sad
0 Kudos
Message 1 of 2
(3,012 Views)
This is a duplicate post.  See this thread.
Pat P.
Software Engineer
National Instruments
0 Kudos
Message 2 of 2
(2,990 Views)