Chloe
Chloe

Reputation: 1

How to round up in C

#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?

Upvotes: 0

Views: 3424

Answers (6)

Luis Colorado
Luis Colorado

Reputation: 12668

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.

Upvotes: 0

David Grayson
David Grayson

Reputation: 87386

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");
}

Upvotes: 0

chux
chux

Reputation: 153348

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));

Upvotes: 1

Ry-
Ry-

Reputation: 224886

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);

Upvotes: 0

Jeff Dege
Jeff Dege

Reputation: 11700

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.

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201439

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

Upvotes: 1

Related Questions