Add proper US-style number formatting with comma separators and $ prefix for all prices

This commit is contained in:
Chad 2026-03-29 15:49:25 -05:00
parent c7193bfda5
commit 29a65760c7
6 changed files with 23771 additions and 23706 deletions

File diff suppressed because it is too large Load diff

View file

@ -127,6 +127,49 @@ double spxPrice = 0;
unsigned long lastRefresh = 0;
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) {
WiFiClientSecure client;
client.setInsecure();
@ -239,13 +282,13 @@ void drawDashboard() {
lcd.setTextColor(TFT_WHITE, TFT_BLACK);
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(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(silverPrice > 0 ? "$" + String((long)silverPrice) : "...", col1X, startY + 180);
lcd.drawString(formatPriceUS(silverPrice, 2, true), col1X, startY + 180);
// Column 2: STOCKS
lcd.setTextColor(TFT_ORANGE, TFT_BLACK);
@ -253,10 +296,10 @@ void drawDashboard() {
lcd.setTextColor(TFT_WHITE, TFT_BLACK);
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(strtPrice > 0 ? "$" + String(strtPrice, 2) : "...", col2X, startY + 120);
lcd.drawString(formatPriceUS(strtPrice, 2, true), col2X, startY + 120);
// Column 3: INDICES
lcd.setTextColor(TFT_ORANGE, TFT_BLACK);
@ -264,13 +307,13 @@ void drawDashboard() {
lcd.setTextColor(TFT_WHITE, TFT_BLACK);
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(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(spxPrice > 0 ? String((long)spxPrice) : "...", col3X, startY + 180);
lcd.drawString(formatPriceUS(spxPrice, 2, true), col3X, startY + 180);
// Status bar
lcd.fillRect(0, SCREEN_HEIGHT - 30, SCREEN_WIDTH, 30, TFT_GRAY);