Hello,
Could anybody please help me soon:
In C++, a number x= 367.1234444444444444444. I only want to get x=367.123 or x=367.1234 (some digits after the sign ".") Which function to get it?

Thank you in advance.
Pedro

Perhaps something like this?

If you want to display it with only three precision decimals you should use

cout << setprecision(3) << x;

. You should also include the <iomanip> header.

Would something like this work for you :

float cut(float num, int maxDigitsAllowedAfterDecimal){
 //error checking if you want
 int wholePart = num;
 if(maxDigitsAllowedAfterDecimal == 0) return wholePart;
 float decimalPart = wholePart - num;
 long factor = pow(10,maxDigitAllowedAfterDecimal);
 return wholePart + float(( long((decimalPart*factor))/factor));
 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.