3

Just starting out C. For instance given the number float $25.50. I want to display the dollars and cents separately. So for dollars I can use %.0f to display 25. How can I only display the 50?

Cigaro
  • 51
  • 1
  • 7
  • 1
    Please show us your entire code with current output and expected output – Rizier123 Jan 23 '15 at 16:27
  • `cents = money - floor(money)`? – Marc B Jan 23 '15 at 16:28
  • 1
    would `printf(%2.2f, value)` work? – TZHX Jan 23 '15 at 16:29
  • 1
    How do you want to handle negative numbers (or do you at all)? Also, you're probably setting yourself up for some pain using floating point arithmetic with amounts of money (because floating point arithmetic is imprecise). Consider using an `int` or `long` that contains the amount in cents instead of a float that contains it in dollars. – Wintermute Jan 23 '15 at 16:32
  • 1
    related question: http://stackoverflow.com/questions/8345581/c-printf-a-float-value -- I don't have access to a C compiler at the moment so I can't test if you can have `printf()` truncate numbers before the decimal point though. – TZHX Jan 23 '15 at 16:34
  • Try `man modf` for starters. – twalberg Jan 23 '15 at 18:50

4 Answers4

2

You're probably looking for modf() from <math.h>. That will split the value into the integer part and the fractional part.

double amount = 25.50;
double dollars;
double cents = modf(amount, &dollars);

You can then print just the fraction:

printf("%.2f", cents);

or, if you're not going to need the fractional value again:

printf("%.2f", modf(amount, &dollars));

There isn't a way using just printf() to print only the fractional part of a number; you have to modify the value passed to it for printing so that it only sees the fractional part.

(Note that you should round the value before splitting it, as shown by chux in his answer.)

For many plausible money values, you could probably use cents = amount - (int64_t)amount or something similar — but the better way to do it is with modf().

(Beware: there's also the fmod() function for calculating the modulus of two double values, the floating point analogue of the % operator.)

Also, do not use float for real monetary calculations — at least, not accounting style calculations. You can more or less represent values up to $99,999.99 reliably to 2 decimal places, but anything much beyond that is unreliable. (In one casual test, adding 0.01 to 262144.00 didn't change the result.) If you must use floating point numbers, then use double.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

Round to the nearest cent.
Break into whole and fractional parts.
Print

 float amount = 25.50;

 amount = roundf(amount*100.0f)/100.0f;
 float dollars, cents;
 cents = modff(amount, &dollars);
 printf("%.0f Dollars  %.0f Cents\n", dollars, cents*100);

Rounding is important to deal with amounts like 1.996 printing as "1 Dollars 100 cents".

modf() nicely separates the fraction from the whole number parts of a floating point number .
Use modf() for double.

Jonathan Leffler is on the money about using double vs. float concerning currency.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

When you deal with money with numbers, even if it's just the background of your fun, you really shouldn't have any floating point data type involved. You should use all integers. Not even mention the unreasonability of using float to represent it.

For example:

struct money 
{ 
    long dollars, cents;
}

but normally, money is handled directly from the minimal unit:

typedef money long;

and represent money from cents. When you want dollars:

long dollar_part(money m)
{
    return m / 100;
}

cents:

long cent_part(money m)
{
    return m % 100;
}

so:

money cash;
/* ... */
printf("$ld dollars, $2ld cents", dollar_part(cash), cent_part(cash));
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jason Hu
  • 6,239
  • 1
  • 20
  • 41
  • Avoiding fp works fine for additional/subtraction, But how would one code the [monthly payment on a mortgage formula](http://en.wikipedia.org/wiki/Mortgage_loan#Capital_and_interest)? – chux - Reinstate Monica Jan 23 '15 at 17:05
  • 1
    There are also things like unit costs, where perhaps washers are $1.00 per thousand, so the unit price is $0.001. That is, you sometimes you need smaller units than cents. That's not to deny your basic thesis (floating point in general, `float` in particular, is unsuitable for monetary calculations). Ideally, you'd be able to use the 128-bit decimal floating point defined in IEEE 754:2008, and implemented on IBM Power systems. See [General Decimal Arithmetic](http://speleotrove.com/decimal/) for a treasure trove of information on both binary and decimal floating point arithmetic. – Jonathan Leffler Jan 23 '15 at 18:00
-1

Once you use the mod operator and get the 25, just subtract that value from the original to get the remaining cent value as a float. Multiply by 100 to get the decimal.

Example: 25.50

Methodology:

  1. Get the first part of the number (i.e. 25)
  2. 25.50-25 = .50
  3. .50 * 100 = 50 -> store in variable for later display
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Jfherrm
  • 11
  • 4