10

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?
Dave Mateer
  • 17,608
  • 15
  • 96
  • 149

5 Answers5

9

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 the GNU Multiple Precision Arithmetic Library.

[EDIT:] A better choice than the above library might be qdecimal. It wraps an IEEE-compliant decimal float, which is very similar (but not exactly the same as) the .NET decimal, as well utilizing Qt idioms and practices.

Zero
  • 11,593
  • 9
  • 52
  • 70
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
2

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
  • 87,561
  • 32
  • 179
  • 238
1

No idea about Qt, but there is a decimal floating point library from Intel.

Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88
1

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
  • 11
  • 1
1

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

martin
  • 11
  • 1