Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: Arithmetic Operations | Introduction
C++ Data Types

bookChallenge: Arithmetic Operations

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

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.

Tarefa

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 display the result with two decimal places.

All the code should be implemented inside the convertToEuro function.

  1. Initialize the variable rate with the value 0.94.
  2. Assign to the existing variable euros the result of multiplying dollars by rate.
  3. Use std::fixed to ensure that only digits after the decimal point are displayed.
  4. Use std::setprecision to display the result with exactly two decimal places.

Solução

solution.cpp

solution.cpp

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you show me an example of using std::setprecision and std::fixed?

What happens if I use std::setprecision without std::fixed?

How do I include the necessary headers to use std::setprecision?

close

Awesome!

Completion rate improved to 4.35

bookChallenge: Arithmetic Operations

Deslize para mostrar o menu

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

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.

Tarefa

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 display the result with two decimal places.

All the code should be implemented inside the convertToEuro function.

  1. Initialize the variable rate with the value 0.94.
  2. Assign to the existing variable euros the result of multiplying dollars by rate.
  3. Use std::fixed to ensure that only digits after the decimal point are displayed.
  4. Use std::setprecision to display the result with exactly two decimal places.

Solução

solution.cpp

solution.cpp

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2
single

single

some-alt