All Projects → vpiotr → Decimal_for_cpp

vpiotr / Decimal_for_cpp

Decimal data type for C++

Labels

Projects that are alternatives of or similar to Decimal for cpp

Openwallet Android
The first truly free, libre, and open source light wallet for multiple cryptocurrencies (Bitcoin, Ethereum, Ripple, etc).
Stars: ✭ 86 (-45.91%)
Mutual labels:  currency
Moeda
💰 📈 A foreign exchange rates and currency conversion using CLI
Stars: ✭ 113 (-28.93%)
Mutual labels:  currency
Cryptocurrency Portfolio
Google Sheets automatic creation with Google Apps Script (GAS) for managing a cryptocurrency tracking spreadsheet with multi exchanges
Stars: ✭ 134 (-15.72%)
Mutual labels:  currency
Bitcore
BitCore (BTX) - Cryptocurrency 220 Byte Datacarriersize
Stars: ✭ 94 (-40.88%)
Mutual labels:  currency
Currency
Handles currency calculations, storage etc
Stars: ✭ 109 (-31.45%)
Mutual labels:  currency
Javascript Number Formatter
Lightweight & Fast JavaScript Number Formatter
Stars: ✭ 119 (-25.16%)
Mutual labels:  currency
Currencyconverter
Utilities for doing currency conversion with the Money library
Stars: ✭ 78 (-50.94%)
Mutual labels:  currency
Track Ip
Advanced Ip Tracker Tool
Stars: ✭ 150 (-5.66%)
Mutual labels:  currency
Nanocurrency Js
🔗 A toolkit for the Nano cryptocurrency, allowing you to derive keys, generate seeds, hashes, signatures, proofs of work and blocks.
Stars: ✭ 113 (-28.93%)
Mutual labels:  currency
Exchanger
🏢 Currency exchange rates framework for PHP
Stars: ✭ 133 (-16.35%)
Mutual labels:  currency
Cromos
Cromos is a tool for downloading legitimate extensions of the Chrome Web Store and inject codes in the background of the application.
Stars: ✭ 103 (-35.22%)
Mutual labels:  currency
Currency
A beautiful currency conversion app written in Kotlin and using Anko. Supports multi-theme with dynamic switching effect.
Stars: ✭ 107 (-32.7%)
Mutual labels:  currency
Sample Currency Converter
A sample currency conversion Progressive Web App
Stars: ✭ 119 (-25.16%)
Mutual labels:  currency
Money Open Exchange Rates
A gem that calculates the exchange rate using published rates from open-exchange-rates. Compatible with the money gem.
Stars: ✭ 87 (-45.28%)
Mutual labels:  currency
Django Prices
Django fields for the prices module
Stars: ✭ 135 (-15.09%)
Mutual labels:  currency
Countries
Countries - ISO 3166 (ISO3166-1, ISO3166, Digit, Alpha-2 and Alpha-3) countries codes and names (on eng and rus), ISO 4217 currency designators, ITU-T E.164 IDD calling phone codes, countries capitals, UN M.49 regions codes, ccTLD countries domains, IOC/NOC and FIFA letters codes, VERY FAST, NO maps[], NO slices[], NO init() funcs, NO external links/files/data, NO interface{}, NO specific dependencies, Databases/JSON/GOB/XML/CSV compatible, Emoji countries flags and currencies support, full support ISO-3166-1, ISO-4217, ITU-T E.164, Unicode CLDR and ccTLD standarts.
Stars: ✭ 85 (-46.54%)
Mutual labels:  currency
Getme
CLI utility for everyday tasks. With getme you get weather, forecast, currency rate, upload files, IP address, word definitions, text translations, internet speed, do google searches, get inspirational quotes and get Chuck Norris jokes
Stars: ✭ 118 (-25.79%)
Mutual labels:  currency
Ng Currency
Currency with AngularJS made easy!
Stars: ✭ 156 (-1.89%)
Mutual labels:  currency
Easymoney
Library for operating with monetary values in JavaScript and Typescript 💵
Stars: ✭ 145 (-8.81%)
Mutual labels:  currency
Frankfurter
💱 Currency data API
Stars: ✭ 123 (-22.64%)
Mutual labels:  currency

About

Decimal data type support, for COBOL-like fixed-point operations on currency/money values.

decimal_for_cpp

Author: Piotr Likus

Created: 03/01/2011

Modified: 30/11/2020

Licence: BSD

Version: 1.17

This data type is designed to perform calculation with on-fly roundings & to support correct compare function (floating-point compare is unreliable).

Values are stored internally using 64-bit integer, so maximum number of digits is 18.

Precision is user-defined, so you can use this data type for currency rates.

To store decimal in file you can use "unbiased" functions or use stream i/o.

Examples

Example usage:

#include "decimal.h"

using namespace dec;
using namespace std;

// the following declares currency variable with 2 decimal points
// initialized with integer value (can be also floating-point)
decimal<2> value(143125);

// displays: Value #1 is: 143125.00
cout << "Value #1 is: " << value << endl;

// declare precise value with digits after decimal point
decimal<2> b("0.11");

// perform calculations as with any other numeric type
value += b;

// displays: Value #2 is: 143125.11
cout << "Value #2 is: " << value << endl;

// automatic rounding performed here
value /= 1000;

// displays: Value #3 is: 143.13
cout << "Value #3 is: " << value << endl;

// integer multiplication and division can be used directly in expression
// when integer is on right side
// displays: Value: 143.13 * 2 is: 286.26
cout << "Value: " << value << " * 2 is: " << (value * 2) << endl;

// to use integer on left side you need to cast it
// displays: Value: 2 * 143.13 is: 286.26
cout << "Value: 2 * " << value << " is: " << (decimal_cast<2>(2) * value) << endl;

// to use non-integer constants in expressions you need to use decimal_cast
value = value * decimal_cast<2>("3.33") / decimal_cast<2>(333.0);

// displays: Value #4 is: 1.43
cout << "Value #4 is: " << value << endl;

// to mix decimals with different precision use decimal_cast
// it will round result automatically
decimal<6> exchangeRate(12.1234);
value = decimal_cast<2>(decimal_cast<6>(value) * exchangeRate);

// displays: Value #5 is: 17.34
cout << "Value #5 is: " << value << endl;

// supports optional strong typing, e.g.
// depending on configuration mixing precision can be forbidden
// or handled automatically
decimal<2> d2("12.03");
decimal<4> d4("123.0103");

// compiles always
d2 += d2;
d2 += decimal_cast<2>(d4);
d4 += decimal_cast<4>(d2);

#if DEC_TYPE_LEVEL >= 2
// potential precision loss
// this will fail to compile if you define DEC_TYPE_LEVEL = 0 or 1
d2 += d4;
#endif

#if DEC_TYPE_LEVEL >= 1
// (possibly unintentional) mixed precision without type casting
// this will fail to compile if you define DEC_TYPE_LEVEL = 0
d4 += d2;
#endif

// for default setup displays: mixed d2 = 417.15
cout << "mixed d2 = " << d2 << endl;
// for default setup displays: mixed d4 = 687.2303
cout << "mixed d4 = " << d4 << endl;

// supports decimal and thousand separator localization
dec::decimal_format format(',', '.');

std::string srcText   = "315499999999999.98";
std::string formatted = "315.499.999.999.999,98";
dec::decimal<2> srcDecimal(srcText);

BOOST_CHECK_EQUAL(dec::toString(srcDecimal, format), formatted);

Supported rounding modes:

  • def_round_policy: default rounding (arithmetic)
  • null_round_policy: round towards zero = truncate
  • half_down_round_policy: round half towards negative infinity
  • half_up_round_policy: round half towards positive infinity
  • half_even_round_policy: bankers' rounding, convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding
  • ceiling_round_policy: round towards positive infinity
  • floor_round_policy: round towards negative infinity
  • round_down_round_policy: round towards zero = truncate
  • round_up_round_policy: round away from zero

In order to use one of these rounding modes you need to declare your decimal variable like this:

dec::decimal<2, half_even_round_policy> a;

and it will perform required rounding automatically - for example during assignment or arithmetic operations.

Testing

In order to test the library:

cd ~/tmp
git clone https://github.com/vpiotr/decimal_for_cpp.git
cd decimal_for_cpp
mkdir _build
cd _build

# to create makefile with test support
cmake ..    
# or
cmake -DBUILD_TESTING=ON ..

# to create makefile without test support (and to avoid Boost unit testing)
cmake -DBUILD_TESTING=OFF ..

# to build or install library (only required for testing)
make all

# to execute all test runners 
make test

# to execute specific runner
./test_runner

Other information

For more examples please see \test directory.

Directory structure:

\doc     - documentation (licence etc.)
\include - headers
\test    - unit tests, Boost-based

Code documentation can be generated using Doxygen: http://www.doxygen.org/

Tested compilers:

  • VS2019 Community (MSVC++ 14.2)
  • gcc 9.3.0

Uses C++11 by default, define DEC_NO_CPP11 symbol if your compiler does not support this standard. To use custom namespace, define DEC_NAMESPACE symbol which should contain your target namespace for decimal type. For full list of configuration options see "Config section" in decimal.h file.

For list of project contributors, currently open issues or latest version see project site: https://github.com/vpiotr/decimal_for_cpp

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].