简体   繁体   中英

Limit floating point precision?

Is there a way to round floating points to 2 points? Eg: 3576.7675745342556 becomes 3576.76 .

round(x * 100) / 100.0

If you must keep things floats:

roundf(x * 100) / 100.0

Flexible version using standard library functions:

double GetFloatPrecision(double value, double precision)
{
    return (floor((value * pow(10, precision) + 0.5)) / pow(10, precision)); 
}

If you are printing it out, instead use whatever print formatting function available to you.

In c++

cout << setprecision(2) << f; 

For rounding to render to GUI, use std::ostringstream

For those of you googling to format a float to money like I was:

#include <iomanip>
#include <sstream>
#include <string>

std::string money_format (float val)
{
    std::ostringstream oss;

    oss << std::fixed << std::setfill ('0') << std::setprecision (2) << val;

    return oss.str();
}
// 12.3456 --> "12.35"
// 1.2 --> "1.20"

You must return it as a string. Putting it back into a float will lose the precision.

乘以 100,四舍五入为整数(无论如何你想要),除以 100。请注意,由于 1/100 不能用浮点精确表示,请考虑保留固定精度整数。

Don't use floats. Use integers storing the number of cents and print a decimal point before the last 2 places if you want to print dollars. Floats are almost always wrong for money unless you're doing simplistic calculations (like naive economic mathematical models) where only the magnitude of the numbers really matters and you never subtract nearby numbers.

try use

std::cout<<std::setprecision(2)<<std::cout<<x;

should works and only 2 digit after the floating point appear.

I didn't find a clean answer that satisfied me since most of the clean answers assume you need to print the result which might not be the case if you are just storing some data to the acceptable resolution:

#include <sstream>

template<typename T>
T toPrecision(T input, unsigned precision)
{
    static std::stringstream ss;

    T output;
    ss << std::fixed;
    ss.precision(precision);
    ss << input;
    ss >> output;
    ss.clear();

    return output;
}

template<unsigned P, typename T>
T toPrecision(T input) { return toPrecision(input, P); }

// compile-time version
double newValue = toPrecision<2>(5.9832346739); // newValue: 5.98
// run-time version
double newValue = toPrecision(3.1415, 2); // newValue: 3.14

You can also add static checks for T and precision (in the case of the compile-time signature).

To limit the precision:
If x is a float, no rounding:
(shift up by 2 decimal digits, strip the fraction, shift down by 2 decimal digits)

((int)(x*100.0)) / 100.0F

Float w/ rounding:

((int)(x*100.0 + 0.5F)) / 100.0F

Double w/o rounding:

((long int)(x*100.0)) / 100.0

Double w/ rounding:

((long int)(x*100.0 + 0.5)) / 100.0

Note: Because x is either a float or a double , the fractional part will always be there. It is the difference between how a # is represented ( IEEE 754 ) and the #'s precision.
C99 supports round()

Try this, it works perfectly

float=3576.7675745342556;
printf("%.2f",float);

change some objects in it to see and learn the code.

yourFloatNumber= Float.Round(yourFloatNumber,2); // In C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM