How to round of decimal values in C++ [duplicate] - c++

This question already has answers here:
Round double to 3 points decimal [duplicate]
(4 answers)
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
With a function, I'm getting these values.. 0.07, 0.038, 0.072, 0.078. Now, I'm trying to apply round of to these values so that they should return like this.
round(val, decimal_place). So that, 0.038 value should return as 0 and rest should come as 0.1. (I can do it using an if condition like limiting with <0.5>. But, want to know. Is there any such function is there in C++ which limit decimal places below 0.5.
It's there in VB by doing like this I think. roundof(value, decimal_place).

You can use setprecision() to limit the decimal place on floating point numbers.
var = 1.0 / 3.0; // returns 0.33333333...
cout << fixed << setprecision(1) << var; // returns 0.3

Related

How I can solve C++ output negative numbers when using modulo? [duplicate]

This question already has answers here:
Fastest way to get a positive modulo in C/C++
(9 answers)
How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers
(16 answers)
Closed 2 years ago.
In a code written by me, I have used both below functions to calculate mod of displayed negative number.
fmod(-10,11)
(-10, 11)
Though the correct answer is 1. It always displays the answer -10 in c++. How I can solve it?
From cppreference.com:
double fmod (double numer, double denom);
The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where n is x/y with its fractional part truncated.
The returned value has the same sign as x and is less than y in magnitude.
In your case it is -10 - (-10)/11 * 11 = -10 - 0 * 11 = -10, which is correct for that implementation of fmod. If you need another answer, you should implement your own version, as modulo is defined in different ways for negative numbers.

why float and double does not identify 0 in decimal places [duplicate]

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Printing the correct number of decimal points with cout
(13 answers)
c++ cout << [double] not printing decimal places
(2 answers)
Closed 3 years ago.
setlocale(LC_ALL, "Portuguese");
double var = 1.0;
cout << var << endl;
system("pause");
return 0;
output:
1
Press any key to continue . . .
float and double only represent the values of numbers. “1” and “1.0” are both numerals for the same number, so 1 is the correct value of the var you set to 1.0. float and double do not represent the original numerals used to set their values, nor do they represent how much accuracy (relative to some ideal mathematical value) is present. The “1” you see as output is a result of default formatting. Other formatting options are available, but you must specify them yourself.

Why does "double i = 1/12;" yields to i = 0? [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 5 years ago.
I think the title says everything. I want to define a variable i as the fraction 1/12. However, i is 0.
double i = 1/12;
std::cout << i; // Output: 0
Or, more specific, I want to calculate a power of something:
im_ = std::pow((1 + i), (1/12)) - 1;
However, the compile evaluates (1/12) as 0 and thus the result is wrong.
Simple because 1/12 is evaluated as integer math, not floating point math.
1/12 becomes 0 because integer math does not take into account the decimal fractions.
To get the expected result you will need to write down the numbers as a floating point literal, like this: 1.0/12.0.
More details can be found here: Why can't I return a double from two ints being divided

For loops goes on forever [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
Somehow, when I run this program, it'll go on forever even though I wrote it in a way that makes it stop when i reaches 10. Please help.
double i;
for(i = 0; i != 10; i+= 0.1){
printf("%.1f\n", i);
}
0.1 cannot be represented accurately as double.
A quick fix is to change the loop condition into i < 10.
Otherwise use loop variable of an integer type, a fixed precision float, or whatever else.
Note, however, that with other decimal increments, notably the negative powers of 2 (0.5, 0.25, etc.), it might work, provided the overall iteration count is not too high.

Floating point rounding which rounds 0.5 to 0 [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 8 years ago.
I did some search on the net. However, there is no solution for those case below.
if the value is less than or equal to 5, drop it and all the digits to the right of it.
if the value is greater than 5, increase by 1 the number to be rounded.
123.4561235 round to 123.456123.
123.4561236 round to 123.456124.
Is there any way to do so?
OK here you go.
Disclaimer: I haven't tested this thoroughly. Don't use this in the production code, and especially for money (you shouldn't use floating point for money at all).
double specialRound(double x, int precision) {
return std::round((static_cast<long long>(x * std::pow(10.0, precision + 1)) - 1) / 10.0 + 0.05)
/ std::pow(10.0, precision);
}
Demo