Alex Rivera | Logout

Decimal type in Qt (C++)

Asked 2010-03-30T18:34:48.977
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?
Edit
Report

1 Answer

3

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.%03u\n", x / 1000, x % 1000);

answered 2010-03-30T18:39:17.140

Your Answer