TruLa
TruLa

Reputation: 1147

Dealing with decimals in C

I am writing an application in C (under Linux) which deals with prices. The program receives price quotes in a form of integer pairs (m, e) so that the resulting price is simply 'm * 10^e'.

I'd like to be able both manipulate the prices with arithmetical operations and use "printf"-like functions for formatting output. I have to do that pretty fast.

What is the fastest way dealing decimals in C? I've read about Decimal Floats in gcc, but their lack of libc support makes them impossible to use in a standard way. Thank you.

Upvotes: 0

Views: 190

Answers (2)

Nathan Day
Nathan Day

Reputation: 6037

If your prices are always at the most to 2 decimal places then you can just use integers to represent the number of cents. printf is also simple with

printf("%d.%02d", price/100, price%100 );

Upvotes: 1

unwind
unwind

Reputation: 399813

You can of course use a library such as GMP, it supports arbitrary floating-point precision so it should be able to deal with your data.

Upvotes: 1

Related Questions