#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
const double TAX = 0.13;
double small = 17.96;
double subTotal = small * 6;
double taxes = subTotal * TAX;
printf("j SubTotal: %9.4lf Tax: %9.4lf \n", (double)subTotal, (double)taxes);
return 0;
}
The required output is
SubTotal: 107.7600 Tax: 14.0100 should come out.
My output is:
SubTotal: 107.7600 Tax: 14.0088
What should I do?
The solution is to not ever use floating point types for currency.
If you have a decimal type available, use it.
If you don't, write one.
Or, at a minimum, use long integers to store cents.
If you want to round to two decimal places, multiply by one hundred add one half, truncate to an int and then divide by one hundred. Like,
double round(double v) {
return ((int)(v * 100 + .5)) / 100.;
}
Then you can call that like
double taxes = round(subTotal * TAX);
And I get (with no other changes)
j SubTotal: 107.7600 Tax: 14.0100
C provides useful rounding functions rint(), round(), nearby(), llround() that well handle rounding corner cases.
To round to the nearest 100th, scale the value by /100.0, round and scale back.
#include <math.h>
double value = ...;
value *= 100.0;
value = round(value); // or rint(), nearby()
value /= 100.0;
At this point, value may not exactly be of the form ddd.dd but will be the closest double to that form. Printing with "%.4f" will then print to the closest 0.0001.
printf("%9.4lf\n", value);
Alternatively, take money and round to the smallest monetary unit - suppose OP wants to the nearest cent (0.01).
long long value_cents = llround(value * 100.0);
printf("%lld.%02d00", amount / 100, abs(amount % 100));
It’s a weird requirement to display four decimal places while rounding to two, but you can satisfy it by displaying two decimal places and hard-coding the characters 00 after it:
printf("j SubTotal: %9.2f00 Tax: %9.2f00 \n", subTotal, taxes);
Do not use floating point numbers for currency because they are tricky and inexact in many cases. Here is a working solution that gives you the exact output you want, but you should know it is strange to print more than two digits after the decimal point if those extra digits are always zero.
The + 99 in the code below is the trick you need to make the calculation always round up, instead of rounding down which is the normal behavior of unsigned integer division.
#include <stdio.h>
#include <stdint.h>
void print_money_with_strange_format(uint64_t amount)
{
printf("%llu.%02llu00", amount / 100, amount % 100);
}
int main(void)
{
uint64_t tax_rate = 13;
uint64_t small = 1796;
uint64_t subtotal = small * 6;
uint64_t tax = (subtotal * tax_rate + 99) / 100;
printf("SubTotal: ");
print_money_with_strange_format(subtotal);
printf(" Tax: ");
print_money_with_strange_format(tax);
printf("\n");
}
If you want to round the output to two decimals, but still producing four on output, just use
printf("%9.2f00", value);
and the value will be rounded to two decimals, and two zeros will be appended to it. But, why the computer should round 88 to 100 in a calculation, when you have not commanded it to round numbers at some place?
To round a number when the first decimal is over .5, you can just add 0.5 and then truncate the result. In case you want to do it at some position, just multiply the number by the required power of ten (in this case 100) and after the truncation, divide it again by that power(100), as proposed by other answers.
Related
float number = 123.8798831;
number=(floorf((number + number * 0.1) * 100.0)) / 100.0;
printf("number = %f",number);
I want to get number = 136.25
But the compiler shows me number = 136.259995
I know that I can write like this printf("number = %.2f",number) ,but I need the number itself for further operation.It is necessary that the number be stored in a variable as number = 136.25
It is necessary that the number be stored in a variable as number = 136.25
But that would be the incorrect result. The precise result of number + number * 0.1 is 136.26787141. When you round that downwards to 2 decimal places, the number that you would get is 136.26, and not 136.25.
However, there is no way to store 136.26 in a float because it simply isn't a representable value (on your system). Best you can get is a value that is very close to it. You have successfully produced a floating point number that is very close to 136.26. If you cannot accept the slight error in the value, then you shouldn't be using finite precision floating point arithmetic.
If you wish to print the value of a floating point number up to limited number of decimals, you must understand that not all values can be represented by floating point numbers, and that you must use %.2f to get desired output.
Round float to 2 decimal places in C language?
Just like you did:
multiply with 100
round
divide by 100
I agree with the other comments/answers that using floating point numbers for money is usually not a good idea, not all numbers can be stored exactly. Basically, when you use floating point numbers, you sacrifice exactness for being able to storage very large and very small numbers and being able to store decimals. You don't want to sacrifice exactness when dealing with real money, but I think this is a student project, and no actual money is being calculated, so I wrote the small program to show one way of doing this.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
double number, percent_interest, interest, result, rounded_result;
number = 123.8798831;
percent_interest = 0.1;
interest = (number * percent_interest)/100; //Calculate interest of interest_rate percent.
result = number + interest;
rounded_result = floor(result * 100) / 100;
printf("number=%f, percent_interest=%f, interest=%f, result=%f, rounded_result=%f\n", number, percent_interest, interest, result, rounded_result);
return EXIT_SUCCESS;
}
As you can see, I use double instead float, because double has more precession and floating point constants are of type double not float. The code in your question should give you a warning because in
float number = 123.8798831;
123.8798831 is of type double and has to be converted to float (possibly losing precession in the process).
You should also notice that my program calculates interest at .1% (like you say you want to do) unlike the code in your question which calculates interest at 10%. Your code multiplies by 0.1 which is 10/100 or 10%.
Here is an example of a function you can use for rounding to x number of decimals.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stddef.h>
double dround(double number, int dp)
{
int charsNeeded = 1 + snprintf(NULL, 0, "%.*f", dp, number);
char *buffer = malloc(charsNeeded);
snprintf(buffer, charsNeeded, "%.*f", dp, number);
double result = atof(buffer);
free(buffer);
return result;
}
int main()
{
float number = 37.777779;
number = dround(number,2);
printf("Number is %f\n",number);
return 0;
}
How to round result to third digit after the third digit.
float result = cos(number);
Note that I want to save the result up to the third digit, no rounding. And no, I don't want to print it with .3f, I need to save it as new value;
Example:
0.00367 -> 0.003
N.B. No extra zeroes after 3 are wanted.
Also, I need to be able to get the 3rd digit. For example if it is 0.0037212, I want to get the 3 and use it as an int in some calculation.
0.00367 -> 0.003
A float can typically represent about 232 different values exactly. 0.00367 and 0.003 are not in that set.
The closest float to 0.00367 is 0.0036700000055134296417236328125
The closest float to 0.003__ is 0.0030000000260770320892333984375
I want to save the result up to the third digit
This goal needs a compromise. Save the result to a float near a multiple of 0.001.
Scaling by 1000.0, truncating and dividing by 1000.0 will work for most values.
float y1 = truncf(x * 1000.0f) / 1000.0f;
The above gives a slightly wrong answer with some values near x.xxx000... and x.xxx999.... Using higher precision can solve that.
float y2 = (float) (trunc(x * 1000.0) / 1000.0);
I want to get the 3 and use it as an int in some calculation.
Skip the un-scaling part and only keep 1 digit with fmod().
int digit = (int) fmod((trunc(x * 1000.0), 10);
digit = abs(digit);
In the end, I suspect this approach will not completely satisfy OP's unstated "use it as an int in some calculation.". There are many subtitles to FP math, especially when trying to use a binary FP, as are most double, in some sort of decimal way.
Perhaps the following will meet OP's goal, even though it does some rounding.:
int third_digit = (int) lround(cos(number)*1000.0) % 10;
third_digit = abs(third_digit);
You can scale the value up, use trunc to truncate toward zero, then scale down:
float result = trunc(cos(number) * 1000) / 1000;
Note that due to the inexact nature of floating point numbers, the result won't be the exact value.
If you're looking to specifically extract the third decimal digit, you can do that as follows:
int digit = (int)(result * 1000) % 10;
This will scale the number up so that the digit in question is to the left of the decimal point, then extract that digit.
You can subtract from the number it's remainder from division by 0.001:
result -= fmod(result, 0.001);
Demo
Update:
The question is updated with very conflicting requirements. If you have an exact 0.003 number, there will be infinite numbers of zeroes after it, and it is a mathematical property of numbers. OTOH, float representation cannot guarantee that every exact number of 3 decimal digits will be represented exactly. To solve this problem you will need to give up on using the float type and switch to a some sort of fixed point representation.
Overkill, using sprintf()
double /* or float */ val = 0.00385475337;
if (val < 0) exit(EXIT_FAILURE);
if (val >= 1) exit(EXIT_FAILURE);
char tmp[55];
sprintf(tmp, "%.50f", val);
int third_digit = tmp[4] - '0';
The following code converts dollars (floating values) into cents (integer values). Here 1 dollar == 100 cents. The code works flawlessly except for one value i.e 4.2. It gives output as 419 instead of 420 for 4.2 as input. Where is the bug?
#include <stdio.h>
int main(void)
{
float change;
do
{
printf("O hai! Enter amount in dollars to convert in cents.\n");
scanf("%f",&change); //accepts input in dollars
} while(change < 0.00);
int i = change * 100; //converts dollars into cents
printf("The equivalent cents are %d\n",i);
}
You have this behavior because of the way a float is represented.
In IEEE 754, 4.2 is actually stored as 4.19999980926513671875 so that when you multiply it by 100 and cast it back to an int, you get 419 (i.e. the integer part of the result).
A solution would be to use
int i = (int) roundf(change * 100);
or
int i = (int) lrintf(change * 100);
with #include <math.h>.
Many floating point values do not have an exact representation in base 2.
You can try adding a very small value to push the value a little bit:
int i = (change + 0.0001) * 100; //converts dollars into cents
As you can see here when you store a value in a float the actual number stored is not always what you expected. This is due to how floats are represented in binary (you can learn more on wiki).
Now, as shown in the 1st link, when you try to store 4.2 in change, you actually store 4.19.... Multiply it by 100, store it inside an int,and you get 419.
Changing your float into double will give you better (double) precision, and in your case, it will yield the expected result
double change; # float changed to double
do
{
printf("O hai! Enter amount in dollars to convert in cents.\n");
scanf("%lf",&change); //accepts input in dollars # "%f" changed to "%lf"
} while(change < 0.00);
int i = change * 100; //converts dollars into cents
printf("The equivalent cents are %d\n",i);
input: 4.2
output: 420
on my machine.
Note that using double is not a fail-safe. It can also give misleading results, but it should behave better than float.
In this case that you know that the decimals for work with money are 2, I suggest that work with float representation with 3 decimals, and then the result must do round up to 2 decimals. Is important be carefully, because the working with money is very delicate.
Hope this help.
I don't understand why doesn't the roundf() function from math.h round the donation variable, whilst it rounds livestockPM without a problem. I need to use the rounded values for other calculations, but I'm using printf to check if the values are correct, and it simply returns wrong values (doesn't round variable donation). Also, the variable final only returns values as if rounded to .00, doesn't matter what variables farmer1,2,3 hold.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
int farmer1 = 9940;
int farmer2 = 4241;
int farmer3 = 7779;
float livestockPM = (float)farmer1 / (float)farmer2;
printf("livestock: %f\n",livestockPM);
livestockPM = roundf(livestockPM * 100) / 100;
printf("livestock rounded: %f\n",livestockPM);
float donation = (float)livestockPM * (float)farmer3;
printf("donation: %f\n", donation);
donation = roundf(donation * 100.00) / 100.00;
printf("donation rounded: %f\n", donation);
float final = donation * (float)farmer2;
printf("final: %f\n", final);
return 0;
}
Output:
livestock: 2.343787
livestock rounded: 2.340000
donation: 18202.859375
donation rounded: 18202.859375
final: 77198328.000000
Anyone got any idea why? I was thinking because of multiplying float with int, but I can't seem to get it work like this. I've tried removing the (float) from integer variables, but the results were undesirable as well. Thanks.
OP's float is encoded using binary floating point and 18202.859375 lacks precision to take on a value that "%f" prints as 18202.860000.
A float cannot represent every possible number. As a binary floating point number it can represent numbers like below. See IEEE 754 Converter, but not in between.
18202.859375
18202.86138125
When the following executes, the best possible result is again 18202.859375.
float donation_rounded = roundf(18202.859375 * 100.00) / 100.00;
Recall that printf("%f\n", x) prints a number rounded textually to the closest 0.000001 value.
Code could use double, but the same problem will occur with very large numbers, but may meet OP''s immediate need. #user3386109
As OP appears to be trying to cope with money, there is no great solution in standard C. best money/currency representation goes into some of the issues.
I'm sure I'm missing something extremely simply since I'm very new to this, but for some reason when I divide an odd number by 2 and then multiply the result, the program will only multiply by the number rounded down, for example when I divide 5 by 2 and then multiply that, it only multiplies it by 2, not even 2.5. What I am eventually trying to do is have it so it rounds up so that dividing 5 by 2 would become 3, but for whatever reason it rounds down now automatically. Here's my code:
#include <stdio.h>
#include <math.h>
int main() {
int num_containers;
float price, total, num_half;
printf("What is the cost of one container?\n");
scanf("%f", &price);
printf("How many containers are you buying?\n");
scanf("%d", &num_containers);
num_half = num_containers / 2;
total = num_half * price;
if(num_containers % 2 == 0)
printf("The total cost is $%.2f.\n", num_containers / 2 * price);
else
printf("The total cost is $%f.\n", num_containers / 2 * price);
return 0;
}
num_containers is defined as int, when you divide it by 2 it is rounded down.
The expression
num_containers / 2;
is evaluated as integer division since both operands are integers, resulting in an integer. There's no way to represent the remaining 0.5 in an integer, so it is lost.
For comparison, the expression
0.5 * num_containers;
includes a floating-point literal, so num_containers is promoted to a double first, and the result is a double (num_containers / 2.0 does the same).
Since everything apart from num_containers is a float, skipping the intermediate value would have worked:
total = num_containers * price / 2;
Note, in order to force rounding up, you can either do it explicitly with ceil
total = ceil(num_containers / 2.0) * price;
or stay in integer land and simply add one:
int more_than_half = (num_containers + 1) / 2;
The variable num_containers is an integer, and so the expression num_containers / 2 will resolve to a rounded integer value. If you declare it as float num_containers, you should get the desired result, or you can cast it as a floating point value whenever you use it in an expression. For example:
num_half = (float) num_containers / 2;
If you want a rounded division in integer arithmetic, add an adjustment before doing the divide. For example to divide by 10 and round, add 5 first. In your case of dividing by 2
num_half = (num_containers + 1) / 2;
Which will round 5 / 2 up to 3.
It's because num_containers is defined as an int. If you do operations including an int and a float, the result will become a float. You're dividing an int by an int, so the result is an int.
If you want a float result, first cast num_containers to a float, or, if you're always dividing by the literal '2', change it to 2.0f.