tags:

views:

446

answers:

5

What is the correct type to use in Qt development (or C++ in general) for decimal arithmetic, i.e. the equivalent of System.Decimal struct in .Net?

  • Does Qt provide a built-in struct? (I can't find it in the docs, but maybe don't know where to look.)
  • Is there a "standard" C++ library to use?
+1  A: 

There is no decimal type in C++, and to my knowledge, there is none provided by Qt either. Depending on your range of values, a decent solution may be to just use an unsigned int and divide/multiply by a power of ten when displaying it.

For example, if we are talking about dollars, you could do this:

unsigned int d = 100; // 1 dollar, basically d holds cents here...

or if you want 3 decimal places and wanted to store 123.456

unsigned int x = 123456; and just do some simply math when displaying:

printf("%u.%u\n", x / 1000, x % 1000);

Evan Teran
+4  A: 

What is the correct type to use in Qt development (or C++ in general) for decimal arithmetic, i.e. the equivalent of System.Decimal struct in .Net?

Neither C++ standard library nor Qt has any data type equivalent to System.Decimal in .NET.

Does Qt provide a built-in struct? (I can't find it in the docs, but maybe don't know where to look.)

No.

Is there a "standard" C++ library to use?

No.

But you might want to have a look at this library.

missingfaktor
Thanks for the confirmation.
Dave
A: 

A quick search turns up that gcc may support decimals directlyand that far more general information is available at for instance http://speleotrove.com/decimal/

Rob
A: 

There is also an implementation in C, available from IBM : http://www.alphaworks.ibm.com/tech/decNumber

martin