I am marshalling struct in c# which contain some decimal properties, but I am not able to Unmarshall it in c++ as there is no decimal datatype in c++. As it is a financial application i don't have any other option apart from decimal.Anybody have some work around ?
2 Answers
Use integers.
Either marshall the number of cents (8.25$ becomes 825) or centicents (8.25$ becomes 82500). Not only is this the norm at many financial firms, it also happens to be simple to understand, implement and debug.
It's easy to port, there's really no downsides.
-
1For what it's worth this has been fairly standard in many financial applications for awhile. It's basically using fixed point. You can also go down to the mill if you want to and still handle absurd amounts of money– MgetzJul 15, 2019 at 13:12
You could use Boost.Multiprecision:
The Multiprecision Library provides integer, rational and floating-point types in C++ that have more range and precision than C++'s ordinary built-in types.
-
I am seen boost Multiprecision but it accept char i.e. char data[50] = "522780.171000000000000000"; cpp_dec_float_100 decimal(data); I have to accept the incoming data in to char and then I will have to convert in to boost precision which will reduce my throughput. Jul 15, 2019 at 13:10
-
@jaychanda You can write code to do the conversion directly. Example:
cpp_dec_float_100 decimal; for (auto digit : ...) { decimal *= 10; decimal += digit; }
then handle digits after decimal point. Jul 15, 2019 at 13:13
long
in C# parlance and count in cents