0.1.5 Currency

This commit is contained in:
rob tillaart 2021-12-15 11:20:58 +01:00
parent dfb18b8083
commit 3f5e88dc61
7 changed files with 49 additions and 35 deletions

View File

@ -8,7 +8,7 @@
# Currency
Arduino library to help formatting integers for printing as currency
Arduino library to help formatting integers for printing as currency.
## Warning: experimental
@ -23,7 +23,7 @@ The currency functions assume you do the currency math in integer units.
For dollars and euro's this would be cents. For numbers with more decimals
it is a smaller unit.
Using integers makes addition, subtraction and multiplication exact.
Using integers makes addition, subtraction and multiplication of currency exact.
The library has experimental wrappers for float/double values.
Not tested extensively yet.
@ -72,7 +72,9 @@ The following functions are implemented:
### float Wrapper functions
Even more experimental - not tested
Experimental - not tested
All assumes 2 decimals except bitcoin which has 6.
- **char \* bitcoinf(double value)**
- **char \* dollarf(double value)**
@ -85,17 +87,24 @@ Even more experimental - not tested
## Operation
See examples
See examples.
## Performance
Performance is hard to optimize. Most time is spend in splitting
individual digits (div / mod 10).
## Future
- update documentation.
- performance test + optimize.
- More wrapper functions?
- test double parameters.
- should decimals be a parameter too?
- add BTC, USD, EUR, GBP, RUB, JPY, CNY, etc. (3+1 chars)
- currency conversion?
- intern all in ???
- https://www.easymarkets.com/eu/learn-centre/discover-trading/currency-acronyms-and-abbreviations/
**won't**
- currency conversion?
- intern all in ???

View File

@ -3,7 +3,7 @@
//
// FILE: currency.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.1.5
// PURPOSE: Currency library for Arduino
// URL: https://github.com/RobTillaart/Currency
@ -14,12 +14,13 @@
// added pound, renamed roubles
// 0.1.3 2021-12-14 update library.json, license, minor edits.
// 0.1.4 2021-12-14 fix version number for PlatformIO
// 0.1.5 2021-12-15 improve performance / size
#include "Arduino.h"
#define CURRENCY_VERSION (F("0.1.4"))
#define CURRENCY_VERSION (F("0.1.5"))
// TODO
@ -34,7 +35,7 @@
char * currency(int32_t value, int decimals, char decimalSeparator, char thousandSeparator, char symbol)
{
static char tmp[16];
int index = 0;
uint8_t index = 0;
int32_t v = value;
bool negative = v < 0;
@ -46,13 +47,11 @@ char * currency(int32_t value, int decimals, char decimalSeparator, char thousan
{
// separators
if ((pos == 0) && (decimals > 0) ) tmp[index++] = decimalSeparator;
if ((pos > 0) && (pos % 3 == 0) && (v > 0)) tmp[index++] = thousandSeparator;
// TODO can be optimized
int digit = (v % 10) + '0';
v /= 10;
tmp[index++] = digit;
if ((pos > 0) && (pos % 3 == 0) ) tmp[index++] = thousandSeparator;
pos++;
tmp[index++] = (v % 10) + '0';
v /= 10;
}
if (negative) tmp[index++] = '-';
else tmp[index++] = ' ';
@ -60,12 +59,11 @@ char * currency(int32_t value, int decimals, char decimalSeparator, char thousan
tmp[index] = '\0';
// reverse string
int length = strlen(tmp); // optimize index is strlen?
for (int i = 0; i < length / 2; i++) // optimize j--
for (uint8_t i = 0, j = index - 1; i < index / 2; i++, j--)
{
char c = tmp[i];
tmp[i] = tmp[length - i - 1];
tmp[length - i - 1] = c;
tmp[i] = tmp[j];
tmp[j] = c;
}
return tmp;
}
@ -86,12 +84,11 @@ char * currency64(int64_t value, int decimals, char decimalSeparator, char thous
{
// separators
if ((pos == 0) && (decimals > 0) ) tmp[index++] = decimalSeparator;
if ((pos > 0) && (pos % 3 == 0) && (v > 0)) tmp[index++] = thousandSeparator;
int digit = (v % 10) + '0';
v /= 10;
tmp[index++] = digit;
if ((pos > 0) && (pos % 3 == 0) ) tmp[index++] = thousandSeparator;
pos++;
tmp[index++] = (v % 10) + '0';
v /= 10;
}
if (negative) tmp[index++] = '-';
else tmp[index++] = ' ';
@ -99,12 +96,11 @@ char * currency64(int64_t value, int decimals, char decimalSeparator, char thous
tmp[index] = '\0';
// reverse string
int length = strlen(tmp);
for (int i = 0; i < length / 2; i++)
for (uint8_t i = 0, j = index - 1; i < index / 2; i++, j--)
{
char c = tmp[i];
tmp[i] = tmp[length - i - 1];
tmp[length - i - 1] = c;
tmp[i] = tmp[j];
tmp[j] = c;
}
return tmp;
}

View File

@ -2,7 +2,7 @@
// FILE: currency_performance.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: performan
// PURPOSE: performance test
// DATE: 2021-12-14
// URL: https://github.com/RobTillaart/currency

View File

@ -0,0 +1,10 @@
currency_performance.ino
TIME: 436
$ 10,000,000
TIME: 2340
$-9,999,999,999,999.99
done...

View File

@ -1,10 +1,9 @@
//
// FILE: print_currency.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: display an int as currency
// PURPOSE: display an int as currency test
// DATE: 2021-02-28
// URL: https://github.com/RobTillaart/printFormatters
// URL: https://github.com/RobTillaart/currency
#include "Arduino.h"

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/currency"
},
"version": "0.1.4",
"version": "0.1.5",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=currency
version=0.1.4
version=0.1.5
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library to help formatting integers as currency e.g. $ 1.000.000,00.