C - Use integer values for calculations involving money

Introduction

Floating-point variables aren't ideal for calculations involving money because of the potential rounding.

We can use long and integer to store cents and dollar

Demo

#include <stdio.h>

int main(void)
{
    const long unit_price = 350L;                        // Unit price in cents
    int quantity = 0;
    printf("Enter the number that you want to buy:");    // Prompt message
    scanf(" %d", &quantity);                             // Read the input

    long discount = 0L;                                  // Discount allowed
    if(quantity > 10)
      discount = 5L;                                     // 5% discount
    long total_price = quantity*unit_price*(100-discount)/100;
    long dollars = total_price/100;
    long cents = total_price%100;
    printf("\nThe price for %d is $%ld.%ld\n", quantity, dollars, cents);
    return 0;//w  w w.j  a  v  a2 s . c om
}

Result

Related Topic