From c9c2237ee48d29b2872d64ee07f39a47d04cde10 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Wed, 8 Jan 2025 09:31:46 -0800 Subject: [PATCH] indexticker: fix rendering of 0.0x% Any number that's 0.0x%, for example 0.09%, ends up rendering out as `0.9%`. Fix this by using the humanize package's float() to do the hard work for us. --- apps/indexticker/index_ticker.star | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/apps/indexticker/index_ticker.star b/apps/indexticker/index_ticker.star index fd4a41923..5ab84409d 100644 --- a/apps/indexticker/index_ticker.star +++ b/apps/indexticker/index_ticker.star @@ -12,6 +12,7 @@ v1.3 - Added NIKKEI, Europe 350, Global 100, Global 1200, NZX50 indices; support load("animation.star", "animation") load("encoding/json.star", "json") load("http.star", "http") +load("humanize.star", "humanize") load("math.star", "math") load("render.star", "render") load("schema.star", "schema") @@ -60,27 +61,9 @@ def main(config): Current = INDEX_JSON["chart"]["result"][0]["meta"]["regularMarketPrice"] PointsDiff = Current - LastClose - PercentDiff = PointsDiff / LastClose - StrPercentDiff = str(int(math.round(PercentDiff * 10000))) - - # if % greater than 0 - # elif % between 0 and -1 - # elif % less than 0 - - if PercentDiff > 0: - StrPercentDiff = (StrPercentDiff[0:-2] + "." + StrPercentDiff[-2:]) - elif PercentDiff > -0.01: - StrPercentDiff = StrPercentDiff.replace("-", "-0") - StrPercentDiff = (StrPercentDiff[0:-2] + "." + StrPercentDiff[-2:]) - elif PercentDiff < 0: - StrPercentDiff = (StrPercentDiff[0:-2] + "." + StrPercentDiff[-2:]) - - if StrPercentDiff.startswith("."): - StrPercentDiff = "0" + StrPercentDiff - DiffColor = "#00ff00" - elif StrPercentDiff.startswith("-"): - StrPercentDiff = StrPercentDiff[1:] - StrPercentDiff = "-" + StrPercentDiff + PercentDiff = PointsDiff / LastClose * 100.0 + + if PercentDiff < 0: DiffColor = "#f00" if RangeSelection == "5m&range=1d": @@ -97,8 +80,7 @@ def main(config): Interval = "YTD" if DisplaySelection == "true": - StrPercentDiff = StrPercentDiff + "%" - DisplayDiff = StrPercentDiff + DisplayDiff = humanize.float("#.##", PercentDiff) + "%" else: DisplayDiff = str(PointsDiff)[:6]