mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.3 Currency
This commit is contained in:
parent
87ad71de4d
commit
5eb07ec927
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-2021 Rob Tillaart
|
||||
Copyright (c) 2021-2022 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -6,7 +6,6 @@
|
||||
[![GitHub release](https://img.shields.io/github/release/RobTillaart/currency.svg?maxAge=3600)](https://github.com/RobTillaart/currency/releases)
|
||||
|
||||
|
||||
|
||||
# Currency
|
||||
|
||||
Arduino library to help formatting integers for printing as currency
|
||||
@ -14,6 +13,7 @@ Arduino library to help formatting integers for printing as currency
|
||||
## Warning: experimental
|
||||
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
The currency library contains a number of functions that help to print
|
||||
@ -33,8 +33,8 @@ of minimum and maximum values. When large amounts are needed one can
|
||||
use the currency64() or one of its derived formatters as this is based
|
||||
upon int64_t numbers.
|
||||
|
||||
There is a relation with the printhelpers class - https://github.com/RobTillaart/printHelpers
|
||||
When this library has matured it might be merged with it.
|
||||
There is a relation with the printHelpers class - https://github.com/RobTillaart/printHelpers
|
||||
When this currency library has matured it might be merged with printHelpers.
|
||||
|
||||
|
||||
## Interface
|
||||
@ -44,8 +44,8 @@ The following functions are implemented:
|
||||
|
||||
### Core function
|
||||
|
||||
- **char \* currency(int32_t value, uint8_t decimals, char decimalseparator, char thousandseparator, char symbol);**
|
||||
- **char \* currency64(int64_t value, uint8_t decimals, char decimalseparator, char thousandseparator, char symbol);**
|
||||
- **char \* currency(int32_t value, uint8_t decimals, char decimalSeparator, char thousandSeparator, char symbol);**
|
||||
- **char \* currency64(int64_t value, uint8_t decimals, char decimalSeparator, char thousandSeparator, char symbol);**
|
||||
|
||||
|
||||
### int32 Wrapper functions
|
||||
@ -83,15 +83,19 @@ Even more experimental - not tested
|
||||
- **char \* yuanf(double value)**
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- More wrapper functions?
|
||||
- test double parameters.
|
||||
- add BTC, USD, EUR, GBP, RUB, JPY, CNY, etc. (3+1 chars)
|
||||
- https://www.easymarkets.com/eu/learn-centre/discover-trading/currency-acronyms-and-abbreviations/
|
||||
|
||||
|
||||
## Operation
|
||||
|
||||
See examples
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- update documentation.
|
||||
- performance test + optimize.
|
||||
- More wrapper functions?
|
||||
- test double parameters.
|
||||
- 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/
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
//
|
||||
// FILE: currency.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.2
|
||||
// VERSION: 0.1.3
|
||||
// PURPOSE: Currency library for Arduino
|
||||
// URL: https://github.com/RobTillaart/Currency
|
||||
|
||||
@ -12,13 +12,13 @@
|
||||
// 0.1.1 2021-05-27 fix library.properties
|
||||
// 0.1.2 2021-10-20 update build-CI + badges
|
||||
// added pound, renamed roubles
|
||||
//
|
||||
// 0.1.3 2021-12-14 update library.json, license, minor edits.
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define CURRENCY_VERSION (F("0.1.2"))
|
||||
#define CURRENCY_VERSION (F("0.1.3"))
|
||||
|
||||
|
||||
// TODO
|
||||
@ -30,79 +30,80 @@
|
||||
// U+20BF = Bitcoin
|
||||
|
||||
|
||||
char * currency(int32_t value, int decimals, char dsep, char tsep, char sym)
|
||||
char * currency(int32_t value, int decimals, char decimalSeparator, char thousandSeparator, char symbol)
|
||||
{
|
||||
static char tmp[16];
|
||||
int idx = 0;
|
||||
int index = 0;
|
||||
|
||||
int32_t v = value;
|
||||
bool neg = v < 0;
|
||||
if (neg) v = -v;
|
||||
bool negative = v < 0;
|
||||
if (negative) v = -v;
|
||||
|
||||
int p = -decimals; // decimal places
|
||||
int pos = -decimals; // decimal places
|
||||
|
||||
while ((p < 1) || ( v > 0))
|
||||
while ((pos < 1) || (v > 0))
|
||||
{
|
||||
// separators
|
||||
if ((p == 0) && (decimals > 0) ) tmp[idx++] = dsep;
|
||||
if ((p > 0) && (p % 3 == 0) && (v > 0)) tmp[idx++] = tsep;
|
||||
if ((pos == 0) && (decimals > 0) ) tmp[index++] = decimalSeparator;
|
||||
if ((pos > 0) && (pos % 3 == 0) && (v > 0)) tmp[index++] = thousandSeparator;
|
||||
|
||||
int d = (v % 10) + '0';
|
||||
// TODO can be optimized
|
||||
int digit = (v % 10) + '0';
|
||||
v /= 10;
|
||||
tmp[idx++] = d;
|
||||
p++;
|
||||
tmp[index++] = digit;
|
||||
pos++;
|
||||
}
|
||||
if (neg) tmp[idx++] = '-';
|
||||
else tmp[idx++] = ' ';
|
||||
tmp[idx++] = sym;
|
||||
tmp[idx] = 0;
|
||||
if (negative) tmp[index++] = '-';
|
||||
else tmp[index++] = ' ';
|
||||
tmp[index++] = symbol;
|
||||
tmp[index] = '\0';
|
||||
|
||||
// reverse string
|
||||
int len = strlen(tmp);
|
||||
for (int i = 0; i < len / 2; i++)
|
||||
int length = strlen(tmp); // optimize index is strlen?
|
||||
for (int i = 0; i < length / 2; i++) // optimize j--
|
||||
{
|
||||
char c = tmp[i];
|
||||
tmp[i] = tmp[len - i - 1];
|
||||
tmp[len - i - 1] = c;
|
||||
tmp[i] = tmp[length - i - 1];
|
||||
tmp[length - i - 1] = c;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
char * currency64(int64_t value, int decimals, char dsep, char tsep, char sym)
|
||||
char * currency64(int64_t value, int decimals, char decimalSeparator, char thousandSeparator, char symbol)
|
||||
{
|
||||
static char tmp[32];
|
||||
int idx = 0;
|
||||
int index = 0;
|
||||
|
||||
int64_t v = value;
|
||||
bool neg = v < 0;
|
||||
if (neg) v = -v;
|
||||
bool negative = v < 0;
|
||||
if (negative) v = -v;
|
||||
|
||||
int p = -decimals; // decimal places
|
||||
int pos = -decimals; // decimal places
|
||||
|
||||
while ((p < 1) || ( v > 0))
|
||||
while ((pos < 1) || (v > 0))
|
||||
{
|
||||
// separators
|
||||
if ((p == 0) && (decimals > 0) ) tmp[idx++] = dsep;
|
||||
if ((p > 0) && (p % 3 == 0) && (v > 0)) tmp[idx++] = tsep;
|
||||
if ((pos == 0) && (decimals > 0) ) tmp[index++] = decimalSeparator;
|
||||
if ((pos > 0) && (pos % 3 == 0) && (v > 0)) tmp[index++] = thousandSeparator;
|
||||
|
||||
int d = (v % 10) + '0';
|
||||
int digit = (v % 10) + '0';
|
||||
v /= 10;
|
||||
tmp[idx++] = d;
|
||||
p++;
|
||||
tmp[index++] = digit;
|
||||
pos++;
|
||||
}
|
||||
if (neg) tmp[idx++] = '-';
|
||||
else tmp[idx++] = ' ';
|
||||
tmp[idx++] = sym;
|
||||
tmp[idx] = 0;
|
||||
if (negative) tmp[index++] = '-';
|
||||
else tmp[index++] = ' ';
|
||||
tmp[index++] = symbol;
|
||||
tmp[index] = '\0';
|
||||
|
||||
// reverse string
|
||||
int len = strlen(tmp);
|
||||
for (int i = 0; i < len / 2; i++)
|
||||
int length = strlen(tmp);
|
||||
for (int i = 0; i < length / 2; i++)
|
||||
{
|
||||
char c = tmp[i];
|
||||
tmp[i] = tmp[len - i - 1];
|
||||
tmp[len - i - 1] = c;
|
||||
tmp[i] = tmp[length - i - 1];
|
||||
tmp[length - i - 1] = c;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
@ -115,7 +116,7 @@ char * bitcoin(int32_t value) { return currency(value, 6, '.', ',', 'B'); }
|
||||
char * dollar(int32_t value) { return currency(value, 2, '.', ',', '$'); }
|
||||
char * euro(int32_t value) { return currency(value, 2, ',', '.', 'E'); }
|
||||
char * pound(int32_t value) { return currency(value, 2, ',', '.', 'L'); }
|
||||
char * roubles(int32_t value) { return currency(value, 2, ',', '.', 'P'); }
|
||||
char * roubles(int32_t value) { return currency(value, 2, ',', '.', 'P'); }
|
||||
char * yen(int32_t value) { return currency(value, 2, '.', ',', 'Y'); }
|
||||
char * yuan(int32_t value) { return currency(value, 2, '.', ',', 'R'); }
|
||||
|
||||
@ -123,7 +124,7 @@ char * bitcoin64(int64_t value) { return currency64(value, 6, '.', ',', 'B');
|
||||
char * dollar64(int64_t value) { return currency64(value, 2, '.', ',', '$'); }
|
||||
char * euro64(int64_t value) { return currency64(value, 2, ',', '.', 'E'); }
|
||||
char * pound64(int64_t value) { return currency64(value, 2, ',', '.', 'L'); }
|
||||
char * roubles64(int64_t value) { return currency64(value, 2, ',', '.', 'P'); }
|
||||
char * roubles64(int64_t value) { return currency64(value, 2, ',', '.', 'P'); }
|
||||
char * yen64(int64_t value) { return currency64(value, 2, '.', ',', 'Y'); }
|
||||
char * yuan64(int64_t value) { return currency64(value, 2, '.', ',', 'R'); }
|
||||
|
||||
@ -131,7 +132,7 @@ char * bitcoinf(double value) { return currency64(round(value * 1000000), 6,
|
||||
char * dollarf(double value) { return currency64(round(value * 100), 2, '.', ',', '$'); }
|
||||
char * eurof(double value) { return currency64(round(value * 100), 2, ',', '.', 'E'); }
|
||||
char * poundf(double value) { return currency64(round(value * 100), 2, ',', '.', 'L'); }
|
||||
char * roublesf(double value) { return currency64(round(value * 100), 2, ',', '.', 'P'); }
|
||||
char * roublesf(double value) { return currency64(round(value * 100), 2, ',', '.', 'P'); }
|
||||
char * yenf(double value) { return currency64(round(value * 100), 2, '.', ',', 'Y'); }
|
||||
char * yuanf(double value) { return currency64(round(value * 100), 2, '.', ',', 'R'); }
|
||||
|
||||
|
@ -0,0 +1,51 @@
|
||||
//
|
||||
// FILE: currency_performance.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: performan
|
||||
// DATE: 2021-12-14
|
||||
// URL: https://github.com/RobTillaart/currency
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "currency.h"
|
||||
|
||||
uint32_t start, stop;
|
||||
char * p;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
start = micros();
|
||||
p = currency(10000000, 0, '.', ',', '$');
|
||||
stop = micros();
|
||||
Serial.print("TIME: ");
|
||||
Serial.println(stop - start);
|
||||
Serial.println(p);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
start = micros();
|
||||
p = currency64(-999999999999999, 2, '.', ',', '$');
|
||||
stop = micros();
|
||||
Serial.print("TIME: ");
|
||||
Serial.println(stop - start);
|
||||
Serial.println(p);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
Serial.println("done...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -0,0 +1,11 @@
|
||||
|
||||
currency_performance.ino
|
||||
|
||||
TIME: 436
|
||||
$ 10,000,000
|
||||
|
||||
TIME: 2364
|
||||
$-9,999,999,999,999.99
|
||||
|
||||
done...
|
||||
|
@ -80,4 +80,6 @@ void loop()
|
||||
{
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Syntax Coloring Map for currency
|
||||
# Syntax Colouring Map for currency
|
||||
|
||||
# Datatypes (KEYWORD1)
|
||||
# Data types (KEYWORD1)
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
currency KEYWORD2
|
||||
@ -29,4 +29,5 @@ yuanf KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
CURRENCY_VERSION LITERAL1
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/currency"
|
||||
},
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.3",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=currency
|
||||
version=0.1.2
|
||||
version=0.1.3
|
||||
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.
|
||||
|
@ -38,8 +38,10 @@
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
fprintf(stderr, "CURRENCY_VERSION: %s\n", (char *) CURRENCY_VERSION);
|
||||
}
|
||||
|
||||
|
||||
unittest_teardown()
|
||||
{
|
||||
}
|
||||
@ -47,8 +49,6 @@ unittest_teardown()
|
||||
|
||||
unittest(currency32)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", CURRENCY_VERSION);
|
||||
|
||||
fprintf(stderr, "%s\n", currency(10000000, 0, '.', ',', '$') );
|
||||
fprintf(stderr, "%s\n", currency(10000000, 1, '.', ',', 'E') );
|
||||
fprintf(stderr, "%s\n", currency(10000000, 2, '.', ',', 'Y') );
|
||||
@ -106,8 +106,6 @@ unittest(currency32)
|
||||
|
||||
unittest(currency64)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", CURRENCY_VERSION);
|
||||
|
||||
assertEqual(0, strcmp("$ 0.00", currency64(0, 2, '.', ',', '$') ) );
|
||||
assertEqual(0, strcmp("$ 9,999,999.99", currency64(999999999, 2, '.', ',', '$') ) );
|
||||
assertEqual(0, strcmp("$-9,999,999.99", currency64(-999999999, 2, '.', ',', '$') ) );
|
||||
@ -116,8 +114,6 @@ unittest(currency64)
|
||||
|
||||
unittest(wrappers32)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", CURRENCY_VERSION);
|
||||
|
||||
assertEqual(0, strcmp("$ 0.00", dollar(0) ) );
|
||||
assertEqual(0, strcmp("$ 9,999,999.99", dollar(999999999) ) );
|
||||
assertEqual(0, strcmp("$-9,999,999.99", dollar(-999999999) ) );
|
||||
@ -130,8 +126,6 @@ unittest(wrappers32)
|
||||
|
||||
unittest(wrappers64)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", CURRENCY_VERSION);
|
||||
|
||||
assertEqual(0, strcmp("$ 0.00", dollar64(0) ) );
|
||||
assertEqual(0, strcmp("$ 9,999,999.99", dollar64(999999999) ) );
|
||||
assertEqual(0, strcmp("$-9,999,999.99", dollar64(-999999999) ) );
|
||||
@ -143,8 +137,6 @@ unittest(wrappers64)
|
||||
|
||||
unittest(wrappersdouble)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", CURRENCY_VERSION);
|
||||
|
||||
assertEqual(0, strcmp("$ 0.00", dollarf(0) ) );
|
||||
assertEqual(0, strcmp("$ 9,999.99", dollarf(9999.99) ) );
|
||||
assertEqual(0, strcmp("$-9,999.99", dollarf(-9999.99) ) );
|
||||
|
Loading…
Reference in New Issue
Block a user