Add proper US-style number formatting with comma separators and $ prefix for all prices
This commit is contained in:
parent
260b489352
commit
5f3212d9ad
1 changed files with 52 additions and 9 deletions
59
src/main.cpp
59
src/main.cpp
|
|
@ -127,6 +127,49 @@ double spxPrice = 0;
|
||||||
unsigned long lastRefresh = 0;
|
unsigned long lastRefresh = 0;
|
||||||
const unsigned long REFRESH_INTERVAL = 300000;
|
const unsigned long REFRESH_INTERVAL = 300000;
|
||||||
|
|
||||||
|
// Format price with US-style commas (e.g., 66,647.29)
|
||||||
|
String formatPriceUS(double price, int decimals = 2, bool includeDollar = true) {
|
||||||
|
if (price <= 0) return "...";
|
||||||
|
|
||||||
|
// Build the number string
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
// Handle the integer part with commas
|
||||||
|
long longPrice = (long)price;
|
||||||
|
String intStr = String(longPrice);
|
||||||
|
int len = intStr.length();
|
||||||
|
int commaPos = len % 3;
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (i > 0 && (i - (len % 3)) % 3 == 0 && (len % 3 != 0 || i > 0)) {
|
||||||
|
result += ",";
|
||||||
|
} else if (len % 3 == 0 && i > 0 && i % 3 == 0) {
|
||||||
|
result += ",";
|
||||||
|
}
|
||||||
|
result += intStr[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add decimals if needed
|
||||||
|
if (decimals > 0) {
|
||||||
|
result += ".";
|
||||||
|
double frac = price - longPrice;
|
||||||
|
for (int d = 0; d < decimals; d++) {
|
||||||
|
frac *= 10;
|
||||||
|
}
|
||||||
|
long fracLong = (long)(frac + 0.5); // Round
|
||||||
|
String fracStr = String(fracLong);
|
||||||
|
while (fracStr.length() < decimals) {
|
||||||
|
fracStr = "0" + fracStr;
|
||||||
|
}
|
||||||
|
result += fracStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (includeDollar) {
|
||||||
|
return "$" + result;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
bool fetchPrice(const char* symbol, double* result) {
|
bool fetchPrice(const char* symbol, double* result) {
|
||||||
WiFiClientSecure client;
|
WiFiClientSecure client;
|
||||||
client.setInsecure();
|
client.setInsecure();
|
||||||
|
|
@ -239,13 +282,13 @@ void drawDashboard() {
|
||||||
|
|
||||||
lcd.setTextColor(TFT_WHITE, TFT_BLACK);
|
lcd.setTextColor(TFT_WHITE, TFT_BLACK);
|
||||||
lcd.drawString("Bitcoin:", col1X, startY + 35);
|
lcd.drawString("Bitcoin:", col1X, startY + 35);
|
||||||
lcd.drawString(btcPrice > 0 ? "$" + String((long)btcPrice) : "...", col1X, startY + 60);
|
lcd.drawString(formatPriceUS(btcPrice, 0, true), col1X, startY + 60);
|
||||||
|
|
||||||
lcd.drawString("Gold:", col1X, startY + 95);
|
lcd.drawString("Gold:", col1X, startY + 95);
|
||||||
lcd.drawString(goldPrice > 0 ? "$" + String((long)goldPrice) : "...", col1X, startY + 120);
|
lcd.drawString(formatPriceUS(goldPrice, 2, true), col1X, startY + 120);
|
||||||
|
|
||||||
lcd.drawString("Silver:", col1X, startY + 155);
|
lcd.drawString("Silver:", col1X, startY + 155);
|
||||||
lcd.drawString(silverPrice > 0 ? "$" + String((long)silverPrice) : "...", col1X, startY + 180);
|
lcd.drawString(formatPriceUS(silverPrice, 2, true), col1X, startY + 180);
|
||||||
|
|
||||||
// Column 2: STOCKS
|
// Column 2: STOCKS
|
||||||
lcd.setTextColor(TFT_ORANGE, TFT_BLACK);
|
lcd.setTextColor(TFT_ORANGE, TFT_BLACK);
|
||||||
|
|
@ -253,10 +296,10 @@ void drawDashboard() {
|
||||||
|
|
||||||
lcd.setTextColor(TFT_WHITE, TFT_BLACK);
|
lcd.setTextColor(TFT_WHITE, TFT_BLACK);
|
||||||
lcd.drawString("MSTR:", col2X, startY + 35);
|
lcd.drawString("MSTR:", col2X, startY + 35);
|
||||||
lcd.drawString(mstrPrice > 0 ? "$" + String(mstrPrice, 2) : "...", col2X, startY + 60);
|
lcd.drawString(formatPriceUS(mstrPrice, 2, true), col2X, startY + 60);
|
||||||
|
|
||||||
lcd.drawString("STRC:", col2X, startY + 95);
|
lcd.drawString("STRC:", col2X, startY + 95);
|
||||||
lcd.drawString(strtPrice > 0 ? "$" + String(strtPrice, 2) : "...", col2X, startY + 120);
|
lcd.drawString(formatPriceUS(strtPrice, 2, true), col2X, startY + 120);
|
||||||
|
|
||||||
// Column 3: INDICES
|
// Column 3: INDICES
|
||||||
lcd.setTextColor(TFT_ORANGE, TFT_BLACK);
|
lcd.setTextColor(TFT_ORANGE, TFT_BLACK);
|
||||||
|
|
@ -264,13 +307,13 @@ void drawDashboard() {
|
||||||
|
|
||||||
lcd.setTextColor(TFT_WHITE, TFT_BLACK);
|
lcd.setTextColor(TFT_WHITE, TFT_BLACK);
|
||||||
lcd.drawString("NASDAQ:", col3X, startY + 35);
|
lcd.drawString("NASDAQ:", col3X, startY + 35);
|
||||||
lcd.drawString(nasdaqPrice > 0 ? String((long)nasdaqPrice) : "...", col3X, startY + 60);
|
lcd.drawString(formatPriceUS(nasdaqPrice, 2, true), col3X, startY + 60);
|
||||||
|
|
||||||
lcd.drawString("DOW:", col3X, startY + 95);
|
lcd.drawString("DOW:", col3X, startY + 95);
|
||||||
lcd.drawString(dowPrice > 0 ? String((long)dowPrice) : "...", col3X, startY + 120);
|
lcd.drawString(formatPriceUS(dowPrice, 2, true), col3X, startY + 120);
|
||||||
|
|
||||||
lcd.drawString("SPX:", col3X, startY + 155);
|
lcd.drawString("SPX:", col3X, startY + 155);
|
||||||
lcd.drawString(spxPrice > 0 ? String((long)spxPrice) : "...", col3X, startY + 180);
|
lcd.drawString(formatPriceUS(spxPrice, 2, true), col3X, startY + 180);
|
||||||
|
|
||||||
// Status bar
|
// Status bar
|
||||||
lcd.fillRect(0, SCREEN_HEIGHT - 30, SCREEN_WIDTH, 30, TFT_GRAY);
|
lcd.fillRect(0, SCREEN_HEIGHT - 30, SCREEN_WIDTH, 30, TFT_GRAY);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue