mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.5 Currency
This commit is contained in:
parent
dfb18b8083
commit
3f5e88dc61
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
# Currency
|
# Currency
|
||||||
|
|
||||||
Arduino library to help formatting integers for printing as currency
|
Arduino library to help formatting integers for printing as currency.
|
||||||
|
|
||||||
## Warning: experimental
|
## 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
|
For dollars and euro's this would be cents. For numbers with more decimals
|
||||||
it is a smaller unit.
|
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.
|
The library has experimental wrappers for float/double values.
|
||||||
Not tested extensively yet.
|
Not tested extensively yet.
|
||||||
@ -72,7 +72,9 @@ The following functions are implemented:
|
|||||||
|
|
||||||
### float Wrapper functions
|
### 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 \* bitcoinf(double value)**
|
||||||
- **char \* dollarf(double value)**
|
- **char \* dollarf(double value)**
|
||||||
@ -85,17 +87,24 @@ Even more experimental - not tested
|
|||||||
|
|
||||||
## Operation
|
## Operation
|
||||||
|
|
||||||
See examples
|
See examples.
|
||||||
|
|
||||||
|
|
||||||
|
## Performance
|
||||||
|
|
||||||
|
Performance is hard to optimize. Most time is spend in splitting
|
||||||
|
individual digits (div / mod 10).
|
||||||
|
|
||||||
|
|
||||||
## Future
|
## Future
|
||||||
|
|
||||||
- update documentation.
|
- update documentation.
|
||||||
- performance test + optimize.
|
|
||||||
- More wrapper functions?
|
- More wrapper functions?
|
||||||
- test double parameters.
|
- test double parameters.
|
||||||
|
- should decimals be a parameter too?
|
||||||
- add BTC, USD, EUR, GBP, RUB, JPY, CNY, etc. (3+1 chars)
|
- 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/
|
- https://www.easymarkets.com/eu/learn-centre/discover-trading/currency-acronyms-and-abbreviations/
|
||||||
|
|
||||||
|
**won't**
|
||||||
|
- currency conversion?
|
||||||
|
- intern all in ???
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
//
|
//
|
||||||
// FILE: currency.h
|
// FILE: currency.h
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.4
|
// VERSION: 0.1.5
|
||||||
// PURPOSE: Currency library for Arduino
|
// PURPOSE: Currency library for Arduino
|
||||||
// URL: https://github.com/RobTillaart/Currency
|
// URL: https://github.com/RobTillaart/Currency
|
||||||
|
|
||||||
@ -14,12 +14,13 @@
|
|||||||
// added pound, renamed roubles
|
// added pound, renamed roubles
|
||||||
// 0.1.3 2021-12-14 update library.json, license, minor edits.
|
// 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.4 2021-12-14 fix version number for PlatformIO
|
||||||
|
// 0.1.5 2021-12-15 improve performance / size
|
||||||
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
|
||||||
#define CURRENCY_VERSION (F("0.1.4"))
|
#define CURRENCY_VERSION (F("0.1.5"))
|
||||||
|
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
@ -34,7 +35,7 @@
|
|||||||
char * currency(int32_t value, int decimals, char decimalSeparator, char thousandSeparator, char symbol)
|
char * currency(int32_t value, int decimals, char decimalSeparator, char thousandSeparator, char symbol)
|
||||||
{
|
{
|
||||||
static char tmp[16];
|
static char tmp[16];
|
||||||
int index = 0;
|
uint8_t index = 0;
|
||||||
|
|
||||||
int32_t v = value;
|
int32_t v = value;
|
||||||
bool negative = v < 0;
|
bool negative = v < 0;
|
||||||
@ -46,13 +47,11 @@ char * currency(int32_t value, int decimals, char decimalSeparator, char thousan
|
|||||||
{
|
{
|
||||||
// separators
|
// separators
|
||||||
if ((pos == 0) && (decimals > 0) ) tmp[index++] = decimalSeparator;
|
if ((pos == 0) && (decimals > 0) ) tmp[index++] = decimalSeparator;
|
||||||
if ((pos > 0) && (pos % 3 == 0) && (v > 0)) tmp[index++] = thousandSeparator;
|
if ((pos > 0) && (pos % 3 == 0) ) tmp[index++] = thousandSeparator;
|
||||||
|
|
||||||
// TODO can be optimized
|
|
||||||
int digit = (v % 10) + '0';
|
|
||||||
v /= 10;
|
|
||||||
tmp[index++] = digit;
|
|
||||||
pos++;
|
pos++;
|
||||||
|
|
||||||
|
tmp[index++] = (v % 10) + '0';
|
||||||
|
v /= 10;
|
||||||
}
|
}
|
||||||
if (negative) tmp[index++] = '-';
|
if (negative) tmp[index++] = '-';
|
||||||
else tmp[index++] = ' ';
|
else tmp[index++] = ' ';
|
||||||
@ -60,12 +59,11 @@ char * currency(int32_t value, int decimals, char decimalSeparator, char thousan
|
|||||||
tmp[index] = '\0';
|
tmp[index] = '\0';
|
||||||
|
|
||||||
// reverse string
|
// reverse string
|
||||||
int length = strlen(tmp); // optimize index is strlen?
|
for (uint8_t i = 0, j = index - 1; i < index / 2; i++, j--)
|
||||||
for (int i = 0; i < length / 2; i++) // optimize j--
|
|
||||||
{
|
{
|
||||||
char c = tmp[i];
|
char c = tmp[i];
|
||||||
tmp[i] = tmp[length - i - 1];
|
tmp[i] = tmp[j];
|
||||||
tmp[length - i - 1] = c;
|
tmp[j] = c;
|
||||||
}
|
}
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
@ -86,12 +84,11 @@ char * currency64(int64_t value, int decimals, char decimalSeparator, char thous
|
|||||||
{
|
{
|
||||||
// separators
|
// separators
|
||||||
if ((pos == 0) && (decimals > 0) ) tmp[index++] = decimalSeparator;
|
if ((pos == 0) && (decimals > 0) ) tmp[index++] = decimalSeparator;
|
||||||
if ((pos > 0) && (pos % 3 == 0) && (v > 0)) tmp[index++] = thousandSeparator;
|
if ((pos > 0) && (pos % 3 == 0) ) tmp[index++] = thousandSeparator;
|
||||||
|
|
||||||
int digit = (v % 10) + '0';
|
|
||||||
v /= 10;
|
|
||||||
tmp[index++] = digit;
|
|
||||||
pos++;
|
pos++;
|
||||||
|
|
||||||
|
tmp[index++] = (v % 10) + '0';
|
||||||
|
v /= 10;
|
||||||
}
|
}
|
||||||
if (negative) tmp[index++] = '-';
|
if (negative) tmp[index++] = '-';
|
||||||
else tmp[index++] = ' ';
|
else tmp[index++] = ' ';
|
||||||
@ -99,12 +96,11 @@ char * currency64(int64_t value, int decimals, char decimalSeparator, char thous
|
|||||||
tmp[index] = '\0';
|
tmp[index] = '\0';
|
||||||
|
|
||||||
// reverse string
|
// reverse string
|
||||||
int length = strlen(tmp);
|
for (uint8_t i = 0, j = index - 1; i < index / 2; i++, j--)
|
||||||
for (int i = 0; i < length / 2; i++)
|
|
||||||
{
|
{
|
||||||
char c = tmp[i];
|
char c = tmp[i];
|
||||||
tmp[i] = tmp[length - i - 1];
|
tmp[i] = tmp[j];
|
||||||
tmp[length - i - 1] = c;
|
tmp[j] = c;
|
||||||
}
|
}
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
// FILE: currency_performance.ino
|
// FILE: currency_performance.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.0
|
// VERSION: 0.1.0
|
||||||
// PURPOSE: performan
|
// PURPOSE: performance test
|
||||||
// DATE: 2021-12-14
|
// DATE: 2021-12-14
|
||||||
// URL: https://github.com/RobTillaart/currency
|
// URL: https://github.com/RobTillaart/currency
|
||||||
|
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
currency_performance.ino
|
||||||
|
|
||||||
|
TIME: 436
|
||||||
|
$ 10,000,000
|
||||||
|
|
||||||
|
TIME: 2340
|
||||||
|
$-9,999,999,999,999.99
|
||||||
|
|
||||||
|
done...
|
@ -1,10 +1,9 @@
|
|||||||
//
|
//
|
||||||
// FILE: print_currency.ino
|
// FILE: print_currency.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.0
|
// PURPOSE: display an int as currency test
|
||||||
// PURPOSE: display an int as currency
|
|
||||||
// DATE: 2021-02-28
|
// DATE: 2021-02-28
|
||||||
// URL: https://github.com/RobTillaart/printFormatters
|
// URL: https://github.com/RobTillaart/currency
|
||||||
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/RobTillaart/currency"
|
"url": "https://github.com/RobTillaart/currency"
|
||||||
},
|
},
|
||||||
"version": "0.1.4",
|
"version": "0.1.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"frameworks": "arduino",
|
"frameworks": "arduino",
|
||||||
"platforms": "*",
|
"platforms": "*",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=currency
|
name=currency
|
||||||
version=0.1.4
|
version=0.1.5
|
||||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
maintainer=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.
|
sentence=Arduino library to help formatting integers as currency e.g. $ 1.000.000,00.
|
||||||
|
Loading…
Reference in New Issue
Block a user