Skip to content

Commit

Permalink
indexticker: fix rendering of 0.0x%
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Denton-L committed Jan 8, 2025
1 parent 4e5c304 commit c9c2237
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions apps/indexticker/index_ticker.star
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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":
Expand All @@ -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]

Expand Down

0 comments on commit c9c2237

Please sign in to comment.