Turning a limited Float Into Another Float

Hello. I know in C++ I can do the folowing:
1
2
3
float var1 ;
var1 = 9.12345 ;
printf("%.2f",var1) ;

the output will be 9.12. What if I wanted to save that as another separate float with displaying it on screen? How would I do this? Any help qould be appreciated. Thank you!

Your question is a little bit unclear but I believe you are asking how to save it the variable with it's precision truncated? That is, save 9.12345 as 9.12 (which is 9.12000)?

You need to use doubles do this.

This is how I do it:

1
2
3
4
5
6
7
8
9
10

inline void tools::SetDoublePrecision(double& value, int precision, double units)
{
    double fac = (value < 0.0) ? -1. : 1.;
    value = fabs(value);
    uint64 tmp = static_cast<uint64>((value/units) * pow(10.,precision));
    value = (static_cast<double>(tmp) * pow(10.,-precision))*units;
    value *= fac;
}


or if you want to return a type:

1
2
3
4
5
6
7
8
9

inline double TruncatePrecision(const double& value, const int& precision, const double& units)
{
        double sign = (value < 0.0) ? -1. : 1.;
        double tmpvalue = fabs(value);
        uint64 tmp = static_cast<uint64>((tmpvalue/units) * pow(10.,precision));
        return sign*((static_cast<double>(tmp) * pow(10.,-precision))*units);
 }


You can leave out the "units" variable if you want or set it as a default value of 1.0 in the function prototype. I wrote it this way because certain times I want to truncate depending on units (e.g. if the variable represents 9.12345 meters and stored in the program as 9123.45 millimeters and I want the variable in two decimal places with respect to centimeters)

Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


typedef unsigned int uint32;
typedef unsigned long uint64;

#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>

namespace tools
{
    // ============================================================================
    //                                      Truncate after certain decimal place
    // ============================================================================
    inline double TruncatePrecision(const double& value, const int& precision, const double& units)
    {
        double sign = (value < 0.0) ? -1. : 1.;
        double tmpvalue = fabs(value);
        uint64 tmp = static_cast<uint64>((tmpvalue/units) * pow(10.,precision));
        return sign*((static_cast<double>(tmp) * pow(10.,-precision))*units);
    }

}

int main()
{

    const double mm = 1.0;
    const double cm = 10.0;
    const double m = 1000.0;

    double var1 = 9.12345*m;

    std::cout << std::endl;

    std::cout << "\t" << std::setw(20) << std::setprecision(5) << std::fixed << var1 << std::endl;
    std::cout << "\t" << std::setw(20) << std::setprecision(5) << var1/cm << std::endl;
    std::cout << "\t" << std::setw(20) << std::setprecision(5) << var1/m << std::endl;

    double var_truncated = tools::TruncatePrecision(var1, 2, cm);

    std::cout << std::endl;

    std::cout << "\t" << std::setw(20) << std::setprecision(5) << var_truncated << std::endl;
    std::cout << "\t" << std::setw(20) << std::setprecision(5) << var_truncated/cm << std::endl;
    std::cout << "\t" << std::setw(20) << std::setprecision(5) << var_truncated/m << std::endl;

    std::cout.unsetf(std::ios::fixed);

    std::cout << std::endl;

    return 0;
}


where the output looks like:

1
2
3
4
5
6
7
8
9
10


	          9123.45000
	           912.34500
	             9.12345

	          9123.40000
	           912.34000
	             9.12340


This doesn't work with floats though because float doesn't retain precision in the detail double does. Making var1 and var_truncated floats instead of double results in this:

1
2
3
4
5
6
7
8
9
10


	          9123.45020
	           912.34502
	             9.12345

	          9123.40039
	           912.34004
	             9.12340


Topic archived. No new replies allowed.