static_cast

I am trying to understand why/when I should use static_case. For instance, in this code:

1 // Enter purchase amount
2 double purchase amount;
3 cout << "Enter purchase amount";
4 cin >> purchase amount;

5 double tax = purchaseAmount * 0.06;
6 cout << "Sales tax is " << static_cast<int>(tax*100) / 100.0;

Couldn't we just use cout << "Sales tax is";
cin >> tax;

If this wouldn't produce the correct answer, please explain why. And if I am right then when should we use static_case? Thanks.

Last edited on
Banshee1 wrote:
Couldn't we just use cout << "Sales tax is";
cin >> tax;
I hope to Stroustrup you meant "cout << (tax*100)/100.0;".

The reason is that you want the result of "tax*100" to be a whole number before you divide it by 100.0 again.
Last edited on
static_cast<int>(tax*100) / 100.0

This is just a way of "rounding" a number to the hundredths digit. First, you multiply by 100 to get the two digits you want to keep on the left side of the decimal, then you cast to int to truncate or discard the decimal portion and finally you divide by a 100.0 to move the two digits you saved to the right of the decimal point again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>

using namespace std ;

int main()
{
    // Enter purchase amount
    double purchaseAmount;
    cout << "Enter purchase amount: ";
    cin >> purchaseAmount;

    double tax = purchaseAmount * 0.06;
    cout << "\nSales tax is " << tax ;
    cout << "\nSales tax is " << fixed << setprecision(2) << tax  ;
    cout << "\nSales tax is " << static_cast<int>(tax*100) / 100.0 << '\n' ;
}
Enter purchase amount: 1111.11

Sales tax is 66.6666
Sales tax is 66.67
Sales tax is 66.66

So if you compute line 14 it would be 66.6666 and what we want is a number that only has 2 decimal places(to represent the remaining change).

My next question is why is the output for line 16 only have 2 decimal places. My understanding is you are first turning tax*100 into an integer but then you are dividing it by 100.0 which allows the answer to have a decimal, but why only 2 decimal places? I would guess that both lines 14 and 16 would produce the same output but clearly I am wrong. Thanks for your help.

LB what is stroustrup?, but yea I meant cout.
Last edited on
Bjarne Stroustrup is the guy who created C++. I was making a play on words for the phrase "in god's name".

Bansee1 wrote:
My next question is why is the output for line 16 only have 2 decimal places. My understanding is you are first turning tax*100 into an integer but then you are dividing it by 100.0 which allows the answer to have a decimal, but why only 2 decimal places? I would guess that both lines 14 and 16 would produce the same output but clearly I am wrong.
Try doing the math manually:
66.6666
*100 = 6666.66 (moved decimal two places to right)
to integer = 6666
/100.0 = 66.66 (moved decimal place two places to left)

Recall that when multiplying and dividing by powers of ten you may simply count the number of 0s and move the decimal place accordingly ;)
Last edited on
Ok thanks for the explanation LB. My mistake was when I was doing the math for int I did not convert it to an integer w/ no decimal. Now it makes sense. Thanks.
Topic archived. No new replies allowed.