I Have this code to find out the total purchase price of some numbers and i need to know how to round the numbers to only 2 decimal places.

```
#include <iostream.h>
#include <cstdlib.h>
#include <string.h>
int main() {
float sale_price;
float tax_rate;
float discount_rate;
system("cls");
system("color 07");
cout << "\n\nWelcome to the second version of my total purchase price calculator!\n";
cout << "How much did your recently purchased item cost? ";
cin >> sale_price;
cout << "What is the tax rate in your area? ";
cin >> tax_rate;
cout << "What was the discount rate, if any (if none, just put down 1) ";
cin >> discount_rate;
float tax = sale_price*(tax_rate/100);
float discount = sale_price*(discount_rate/100);
float total_price = sale_price + tax - discount;
cout << "The total price of your item is $"<<total_price<<" with $"<<tax<<" tax minus $"<<discount<<" due to discount.\n";
cout << "Would you like a reciept? y or n. ";
string answer;
End:
cin >> answer;
if (answer == "y") {
goto Reciept;
}
else if (answer == "n") {
return 0;
}
else {
cout << "Try another answer\n";
goto End;
}
Reciept:
system("cls");
system("color 70");
cout << "\xda\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xbf\n";
}
```

this usually gives me 4 decimal places just FYI

Round to 2 decimals: `int((n * 100.0) + 0.5) / 100;`

Round to 3 decimals: `int((n * 1000.0) + 0.5) / 1000;`

etc.

When you output with cout, use the setprecision() operator.

```
cout << fixed;
cout << setprecision(2) << sale_price*(tax_rate/100);
```

Do

```
cout.precision(2);
cout << fixed;
```

before you output the floats.

The easy way is to use `setprecision`

:

```
std::cout << "The total price of your item is $"
<< std::setprecision(2) << total_price ...
```

This will occasionally get things wrong. A better solution is to use an improved rounding function that is not a part of the standard (e.g., How does Excel successfully Rounds Floating numbers even though they are imprecise?). An even better solution is to implement your own fixed point arithmetic algorithm so as to avoid this problem entirely. You can still go one better than that: Use a fixed point arithmetic package that someone else has already written and tested to the nth degree.

You should use Iomanip's setprecision:

Similar Questions

I have a series of strings, which are two numbers: 1.782-100.799 -18.107-102.016 -17.504104.059 How do I split my string after the 3rd decimal place of the first number in each string? so it looks as

Possible Duplicate: Round a double to 2 significant figures after decimal point I need at most N decimals, no more, but I don't want trailing zeroes. For example, if N = 2 then 15.352 15.355 15.3 1

Here is my code: $n = 300; $set = 0; $set2 = 0; for($i = 1; $i<$n; $i++) { for($j = 1; $j <$i; $j++) { $qol = $i % $j; if($qol == 0) { $set += $j; } } for($s=1; $s<$set; $s++) { $qol2 = $set

I have the following code to calculate a certain percentage: var x = 6.5; var total; total = x/15*100; // Result 43.3333333333 What I want to have as result is the exact number 43 and if the total is

My code won't compile due to the error below: The call is ambiguous between the following methods or properties: 'System.Math.Round(double, int)' and 'System.Math.Round(decimal, int) My code is Math.R

I'm trying to associate numbers between two arrays, and one of the arrays contains 500 numbers. Some of the numbers contain 2 decimal places, while others contain 3. How do i read off the numbers as i

In the example below I would like to format to 1 decimal place but python seems to like rounding up the number, is there a way to make it not round the number up? >>> '{:.1%}'.format(0.9995)

Is there a function to round a float in C or do I need to write my own? float conver = 45.592346543; I would like to round the actual value to one decimal place, conver = 45.6.

I was reading How to round decimal value up to nearest 0.05 value??. It mentions the following stuff. Math.ceiling(myValue * 20) / 20 How does this hack work ? I mean how did we arrive at this solut

I'm trying to format a toString to one decimal place but it keeps going to something like 21.0445445454. Here's the line of code please let me know what I'm doing wrong double heightInchToFoot = (12

I use an API that returns a windows DECIMAL type. I need to convert it, the information I need is: the number of decimal place the value itself For example, if my number is 1.234, I would like to ge

I'm currently using the following to compute the difference in two times. The out - in is very fast and thus I do not need to display the hour and minutes which are just 0.00 anyway. How do I actually

How do i get the numeric value of a unicode character in C#? For example if tamil character அ (U+0B85) given, output should be 2949 (i.e. 0x0B85) See also C++: How to get decimal value of a unicode c

Possible Duplicate: c# - How do I round a decimal value to 2 decimal places (for output on a page) how to return decimal with long rest after the point like that: 3.786444499963 to this: 3.787 its

Given a decimal '96.154', how can I ensure that it is always rounded up to 96.16 (as opposed to normal rounding to 2 decimals which would give 96.15).

Possible Duplicate: How do you round to 1 decimal place in Javascript? The following code, displays the total distance covered, on a particular route, displayed on google maps. I managed to convert

How to round a decimal number to nearest 5 or nearest 10 in php

I need a program to convert Binary numbers into Decimal number in Java or in C++. is there some one who can help me.

I want to round the variable retail_price to 2 decimal places. I have tried a few times and it always gives me an error. As far as I know there are two ways to round to 2 decimal places. For this inst

This is my algorithm to find out the speed of my game. self.speed=.7-self.score/50; Now how can I make self.speed round to 2 decimal places?

On a report I have the following code for a field: =Sum([PartQty]*[ModuleQty]) Example results are 2.1 and 2.6. What I need is for these value to round up to the value of 3. How can I change my field

I have a number of decimal numbers stored in a database and need to calculate the percentage increase(or decrease) between two of the numbers using PHP. An example of two of the numbers are: 111.0516

How can I force the NewtonSoft Json Serializer to serialize two decimals numbers the same way? decimal a = 1234.1200M; decimal b = 1234.12M; var sa = JsonConvert.SerializeObject(new { value = a }); va

How can I restrict input to a text-box so that it accepts only numbers and the decimal point?

I need to write a regex that will capture all of the following numbers from a sample text: 2.5 5 0.2 .5 Assuming its not going to be more than 2 digits on either side of the decimal point, what regex

This question already has an answer here: C# decimal take ceiling 2 4 answers How to round up or down in C#? 5 answers Math.Round() will always return results according to mathematics rules

Question I want to round a number to the lowest number based on its 3rd decimal. For an example If the number is 6.1064 I want to output 6.10 rather than 6.11 and if the number is 5.9407 I want to ou

I want to view a byte array in the Eclipse (Helios Release, build id: 20100617-1415) Java debugger as an array of hex bytes (2 digits each) or unsigned decimal numbers? Is that possible? How? For exam

I need to format decimals values in xslt. The template should remove the decimal. And show the number of specified who numbers followed bythe specified decimal positions. The round template round the

This question already has an answer here: JavaScript displaying a float to 2 decimal places 3 answers So I made this quick script that calculates some numbers, but the answer ends with many dec

I want to format BigDecimal Numbers with comma and 2 decimal points. e.g. Amount is: 5.0001 and formatted to: 5.00 Amount is: 999999999.999999 and formatted to: 999,999,999.99 Amount is: 1000.4999 an

int total = 201; double Average = total / 7; result should be 28.71428571428571. I've tried float, decimal and double for Average but the result ends up as 28.

Is there any way to round numbers in C? I do not want to use ceil and floor. Is there any other alternative? I came across this code snippet when I Googled for the answer: (int)(num < 0 ? (num - 0.

I am trying to make a program that converts decimal numbers or text into binary numbers in Perl. The program asks for user input of a character or string and then prints out the result to the console.

how to validate decimal numbers in PHP. I looked at is_numeric() but that won't work for me: bool is_numeric ( mixed var ) Finds whether the given variable is numeric. Numeric strings consist of opti

R x i [Round Reals ] Round double value x to i significant decimal places example: this is scanned from the file: R 3.1415 2 so I need to print out 3.14 doing a big loop, has other functions so far:

How I can fix digits count after point for decimal type WITHOUT ROUNDING in C#. For example: decimal coords = 47.483749999999816; (part of geographic coordinates) I need to stay only 6 digits after po

I have variable like float num = (x/y); I need to round up the result whenever num gives result like 34.443. So how to do this in c#?

I have a table with several columns, with DATA_TYPE FLOAT, NUMBER. There are whole numbers and decimal digits with decimal places. e.g. 234, 4, 0, 23.000000004, 234,4444, ... Assignment: I want that t

This question already has an answer here: How do you round a number to two decimal places in C#? 9 answers I would like to format decimal numbers to the NEXT 0.10 cents (dime) in C# like so: 22

How do I match negative numbers as well by this regular expression? This regex works fine with positive values, but I want it to also allow negative values e.g. -10, -125.5 etc. ^[0-9]\d*(\.\d+)?$ T

may be it is a simple question but I'm try all of conversion method! and it still has error! would you help me? decimal? (nullable decimal) to decimal

Is there a nice way to show show decimal numbers with one decimal place all the time but without rounding? So you have the following numbers: 3.55 3.5 3 and I want them to show 3.5 3.5 3.0

How to retain two decimal points in flex using Math.Round code: txtTotal.text=Math.round((Number(txtRate.text)*Number(txtQuantity.text))).toString(); here the rate is a constant 1.1 and quantity is v

I would like to know the available phone memory, so I wrote this code. File path2 = Environment.getDataDirectory(); StatFs stat2 = new StatFs(path.getPath()); long blockSize2 = stat.getBlockSize(); lo

Can someone please tell me why this doesn't work? I'm confident it's because i'm trying to format a string to two decimal places.. but i don't know how else to make the output rounded to decimal place

I need to mimic the exact functionality of the ceil(), floor() and round() functions on bcmath numbers, I've already found a very similar question but unfortunately the answer provided isn't good enou

I want to be able to enter decimal numbers into a play form. I would like to have the following mapping, but it does not compile. mapping( id -> ignored(NotAssigned:Pk[Long]), date -> date(

How can I round down a number in JavaScript? math.round() doesn't work because it rounds it to the nearest decimal. I'm not sure if there is a better way of doing it other than breaking it apart at t

Possible Duplicate: c# - How do I round a decimal value to 2 decimal places (for output on a page) i have value of 1.564 and 1.565 i need round this value to 1.56 and 1.56,which function suitable fo