(C ++) Как округлить десятичные дроби как 1.05000? - PullRequest
0 голосов
/ 28 июня 2019

У меня проблема с тем, что в моем алгоритме я должен округлить результат до 5 десятичных знаков, включая нули даже после последнего числа.

Единственный тестовый пример, не работающий в моем алгоритме:

Ввод :

молоко 1 4 хлеб мясо молоко aaaaa

Выход :

1.05000 // и мой вывод отображает 1.5

Например;мой результат 1.05 должен отображаться как 1.05000 или 1.2 как 1.20000.Теперь остальная часть алгоритма работает нормально, поэтому единственной проблемой является округление:

 #include <iostream>
 #include <string.h>

 using namespace std;

 int main()
 {

 char name[50];
 cin >> name;
 double price = 0;
 cin >> price;
 int N;
 cin >> N;
 char check_name[50];
 double result = 0;
 bool found = false;
 double result_circle = 0;
 int finally_found = 0

 for (int k = 0; k < N; k++) {
    cin >> check_name;
    for (int i = 0; i < strlen(name); i++) {
        if (name[i] == check_name[i]) {
            found = true;
        } else {
            found = false;
            break;
        }
    }
    if (found) {
        finally_found++;
        break;
    }
 }

 if (finally_found > 0) {
    result = price + (price * 0.05);
 } else {
    result = price + (price * 0.18);
 }

 // here is where the problem starts

 result_circle = result * 1000000; //temporarily expanding the number to the 6th decimal place
 if ((int)result_circle % 10 > 4) { // checking if the 6th decimal place is bigger than 4
    result_circle += 10; // increasing the 5th decimal place
 }

 result_circle = (int)result_circle / 10; // removing the 6th decimal place which we were checking

 cout << (int)result_circle / 100000 << '.' << (int)result_circle % 100000; // here is the main problem, where 105000 % 100000 is seen as 5000 not 05000

    return 0;
 }

Я предполагаю, что главная проблема здесь в том, что «105000% 100000 = 5000», потому что 0к сожалению, после десятичной точки.

Если бы кто-нибудь мог показать самый простой способ решения этой проблемы, было бы здорово.

1 Ответ

0 голосов
/ 03 июля 2019
#include <iomanip> 
 .
 .
 .
cout << fixed << setprecision(5) << result;

Это код, который работал для меня, ответил _Bob.

Полный код:

#include <iostream>
#include <string.h>
#include <iomanip>

using namespace std;

int main()
{

    char name[50];
    cin >> name;
    double price = 0;
    cin >> price;
    int N;
    cin >> N;
    char check_name[50];
    double result = 0;
    bool found = false;
    double result_circle = 0;
    int finally_found = 0;

    for (int k = 0; k < N; k++) {
        cin >> check_name;
        for (int i = 0; i < strlen(name); i++) {
            if (name[i] == check_name[i]) {
                found = true;
            } else {
                found = false;
                break;
            }
        }
        if (found) {
        finally_found++;
    }
}

if (finally_found > 0) {
    result = price + (price * 0.05);
} else {
    result = price + (price * 0.18);
}

cout << fixed << setprecision(5) << result;

return 0;
}
...