Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Arithmetische Operationen | Einführung
C++ Datentypen

bookArithmetische Operationen

Setting the precision

As you already should know you can perform basic data manipulation using arithmetic operators such as addition (+), subtraction (-), division (/), and multiplication (*). Additionally, the modulus operator (%) calculates the remainder of a division.

main.cpp

main.cpp

copy
123456789
#include <iostream> #include <iomanip> int main() { // Uncomment to see the difference // std::cout << std::fixed; std::cout << std::setprecision(5) << 15.125 * 0.8309 << std::endl; }

In the example above, floating-point results may occasionally be produced during calculations. To manage the precision of these results, you can use std::setprecision in combination with std::fixed. This allows you to control the number of digits displayed after the decimal point, ensuring consistent precision in your output.

Note
Note

Without std::fixed, std::setprecision controls the total number of digits displayed, including both before and after the decimal points. With std::fixed, the number is displayed in fixed-point notation, keeping the decimal point in a fixed position.

Aufgabe

Swipe to start coding

You have a variable dollars that stores an amount in US dollars. Your task is to convert this amount to euros and return the result.

All the code should be implemented inside the convertToEuro function.

  1. Initialize the variable rate with the value 0.94.
  2. Calculate the euro value by multiplying dollars by rate.
  3. Use std::fixed and std::setprecision(2) to format the result before returning it.
  4. Return the converted value as a double.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookArithmetische Operationen

Swipe um das Menü anzuzeigen

Setting the precision

As you already should know you can perform basic data manipulation using arithmetic operators such as addition (+), subtraction (-), division (/), and multiplication (*). Additionally, the modulus operator (%) calculates the remainder of a division.

main.cpp

main.cpp

copy
123456789
#include <iostream> #include <iomanip> int main() { // Uncomment to see the difference // std::cout << std::fixed; std::cout << std::setprecision(5) << 15.125 * 0.8309 << std::endl; }

In the example above, floating-point results may occasionally be produced during calculations. To manage the precision of these results, you can use std::setprecision in combination with std::fixed. This allows you to control the number of digits displayed after the decimal point, ensuring consistent precision in your output.

Note
Note

Without std::fixed, std::setprecision controls the total number of digits displayed, including both before and after the decimal points. With std::fixed, the number is displayed in fixed-point notation, keeping the decimal point in a fixed position.

Aufgabe

Swipe to start coding

You have a variable dollars that stores an amount in US dollars. Your task is to convert this amount to euros and return the result.

All the code should be implemented inside the convertToEuro function.

  1. Initialize the variable rate with the value 0.94.
  2. Calculate the euro value by multiplying dollars by rate.
  3. Use std::fixed and std::setprecision(2) to format the result before returning it.
  4. Return the converted value as a double.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2
single

single

some-alt