Function to always round up to the nearest tenth

Pages: 123... 5
Hello,

I'm trying to figure out how to make a C++ function that always rounds up to the nearest tenth. For example:

A value of 20.5 would remain 20.5
A value of 20.51 would become 20.6
A value of 20.55 would become 20.6
And so on...

Unfortunately, the functions provided (ceil and floor) only work for whole number rounding, and almost everybody else uses normal rounding functions instead of always rounding up functions. Also, I would prefer a solution that does not hinder the accuracy of the floats in any way (I'm working with hardware and measuring to at least 3 decimal places).

Thanks.
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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;
int main(){double d = 47.65;
cout << "input d : " << d; cout << endl;

stringstream ss;
cout.precision(10);
ss.precision(10);
ss << d;

bool round = false;
string out = ss.str();
int n, dot = out.find(".");
if(dot != string::npos && ((out.length() - 1 - dot) >= 2) && (round = true)) n = dot + 1 + 1; 
else n = out.length();

out.resize(n);
ss.str(""); ss.clear(); 

ss.str(out);
ss >> d;
if(round) d += 0.1;
cout << "output : " << d << endl;
}
Does that help you? :)

Does it help you? :)
Last edited on
@QuestionMaker2
Your name says it all. You are not here to learn, you are just here to make some random quizzes just for fun, and there is a risk that we spend our time answering your questions just for nothing.

Right? Is it your intention?

Edit: I didn't mean to sound negative (maybe it is funny) but I will be awaiting you with some clarifications.
Last edited on
http://www.cplusplus.com/reference/cmath/ceil/

To round to tenths, multiply the number by 10, call ceil() and divide the result by 10:
1
2
3
4
5
6
7
8
9
10
#include <cmath>
#include <iostream>

int main()
{
    double d;
    while (std::cin >> d) {
        std::cout << d << '\t' << std::ceil(10*d) / 10 << '\n';
    }
}
closedaccount5a8Ym39o6502 wrote:
Does that help you? :)


Does that help you increase your post count? :+D
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
#include <cmath>
#include <iomanip>

int main()
{
    double number = 20.497;
    double d = 0.0;
    
    double kermort = 0;
    double Imhoffs = 0;
    
    int count = 0;
    
    std::cout
    << "   d     kermort Imhoff    diff \n"
    << "--------------------------------\n";
    
    for(int i = 0; i < 1000; i++)
    {
        d = number + i/1000.0;
        
        kermort = (int)(10 * d + 0.99)/10.0;
        Imhoffs = std::ceil(10 * d) / 10;
        
        if(kermort != Imhoffs)
        {
            std::cout << "Error\n";
            count++;
        }
        
        std::cout
        << std::fixed << std::setprecision(4) << d
        << std::setprecision(2) << std::setw(8) << kermort
        << std::setw(8) << Imhoffs
        << std::setw(8) << kermort - Imhoffs
        << '\n';
    }
    
    if(count == 0)
        std::cout << "No difference was found. :)\n";
    else
        std::cout << count << " differences were found. :(\n";
    
    return 0;
}
Last edited on
closed account (48T7M4Gy)
.
Last edited on
closed account (48T7M4Gy)
.
Last edited on
#include <iostream>

int main()
{
double d;
while (std::cin >> d) {
std::cout << d << '\t' << (int)(10*d + .9) / 10.0 << '\n';
}
}

Fails with input 10.001. It returns 10 instead of 10.1

I say again:
1
2
3
4
5
6
7
8
9
10
#include <cmath>
#include <iostream>

int main()
{
    double d;
    while (std::cin >> d) {
        std::cout << d << '\t' << std::ceil(10*d) / 10 << '\n';
    }
}
closed account (48T7M4Gy)
.
Last edited on
Ok, my best solution is this :

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
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>

using namespace std;

int N = 25;
bool first = true;

double random_d(int &precision)
{
 if(first) {
srand(time(NULL)); 
first = false;
} 
int n = (rand() % 9) + 1;
if((rand() % 100) >= 82) n = 1;
 double a = (rand() % 15000);
 double b = 0;
for(int i = 0, j = 0; i < n; i++)
{
 j = (rand() % 9) + 1;
 b = b * 10 + j;
}

precision = ceil(log10(b));

a += (b / pow(10, ceil(log10(b))));

return a;
}

int main()
{
int precision;
double d = random_d(precision);

cout.setf(ios::fixed);
cout.precision(precision);
cout << "in : " << d << endl;

stringstream ss;
ss.setf(ios::fixed);
ss.precision(precision);
ss << d;

bool round = false;
string out = ss.str();
int n, dot = out.find(".");
if(dot != string::npos && ((out.length() - 1 - dot) >= 2) && (round = true)) n = dot + 1 + 1; 
else n = out.length();

out.resize(n);
ss.str(""); ss.clear(); 

ss.str(out);
ss >> d;
if(round) d += 0.1;

cout.precision(1);
cout << "out : " << d << endl << endl;
if(--N > 0) main();
}
It may be a little bit complex but it should do the trick :)
in : 2921.863
out : 2921.9

in : 8902.22153883
out : 8902.3

in : 7527.587767
out : 7527.6

in : 3563.612789185
out : 3563.7

in : 3739.479
out : 3739.5

in : 5455.6
out : 5455.6

in : 6977.231697516
out : 6977.3

in : 4909.65
out : 4909.7

in : 6404.48
out : 6404.5

in : 5815.455781434
out : 5815.5

in : 14128.936
out : 14129.0

in : 7517.19
out : 7517.2

in : 3696.3
out : 3696.3

in : 3950.8
out : 3950.8

in : 10795.553
out : 10795.6

in : 5428.5
out : 5428.5

in : 13446.924974
out : 13447.0

in : 6915.758176514
out : 6915.8

in : 9716.9
out : 9716.9

in : 9619.82342813
out : 9619.9

in : 14781.371545129
out : 14781.4

in : 4073.793768
out : 4073.8

in : 10037.6587189
out : 10037.7

in : 7686.13436637
out : 7686.2

in : 12157.2
out : 12157.2
Does that help? :)
@QuestionMaker
1
2
3
 // Always round up to nearest tenth

d = std::floor(d) + std::ceil((d - std::floor(d)) * 10.0) / 10;


Your very portable solution may look perfect at a glance, but no...

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
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>

using namespace std;

int N = 25;
bool first = true;

double random_d(int &precision)
{
 if(first) {
srand(time(NULL)); 
first = false;
} 
int n = (rand() % 9) + 1;
if((rand() % 100) >= 82) n = 1;
 double a = (rand() % 15000);
 double b = 0;
for(int i = 0, j = 0; i < n; i++)
{
 j = (rand() % 9) + 1;
 b = b * 10 + j;
}

precision = ceil(log10(b));

a += (b / pow(10, ceil(log10(b))));

return a;
}

int main()
{
int precision;
double d = random_d(precision);

cout.setf(ios::fixed);
cout.precision(precision);
cout << "in : " << d << endl;

d = std::floor(d) + std::ceil((d - std::floor(d)) * 10.0) / 10;

cout.precision(1);
cout << "out : " << d << endl << endl;
if(--N > 0) main();
}


To be continued...
Let's see your output results :
in : 1704.8
out : 1704.8

in : 6471.4
out : 6471.4

in : 10689.9286672
out : 10690.0

in : 2623.5337
out : 2623.6

in : 9230.14433
out : 9230.2

in : 1757.7543
out : 1757.8

in : 3655.47612
out : 3655.5

in : 636.3895554
out : 636.4

in : 270.4
out : 270.4

in : 3186.683823
out : 3186.7

in : 10081.577
out : 10081.6

in : 1195.85445
out : 1195.9

in : 10494.189725
out : 10494.2

in : 3329.177
out : 3329.2

in : 4597.38
out : 4597.4

in : 6096.565
out : 6096.6

in : 12594.4
out : 12594.4

in : 729.7
out : 729.8

in : 5794.867
out : 5794.9

in : 6533.8
out : 6533.9 // Wrong!!!

in : 9533.6793976
out : 9533.7

in : 3000.28
out : 3000.3

in : 9815.8241686
out : 9815.9

in : 7902.39
out : 7902.4

in : 3661.24
out : 3661.3
.
Too bad, your very solution does not really comply with your expectations : (
The OP is silent on the number of decimal places and all we can go on is the test data presented rather than making it up.

I doubt whether the esoterica of a 3rd decimal place was envisaged anyway.
But...

I'm working with hardware and measuring to at least 3 decimal places

Just accept that your function is incorrect. Just because it didn't occur to you to test with longer numbers doesn't make it correct.
Pages: 123... 5