Fix markets price precision

This commit is contained in:
Svilen Markov 2025-03-28 14:11:17 +00:00
parent 233de7fc37
commit bd020c93f5
3 changed files with 15 additions and 8 deletions

View File

@ -27,6 +27,9 @@ var globalTemplateFunctions = template.FuncMap{
"formatPrice": func(price float64) string {
return intl.Sprintf("%.2f", price)
},
"formatPriceWithPrecision": func(precision int, price float64) string {
return intl.Sprintf("%."+strconv.Itoa(precision)+"f", price)
},
"dynamicRelativeTimeAttrs": dynamicRelativeTimeAttrs,
"formatServerMegabytes": func(mb uint64) template.HTML {
var value string

View File

@ -17,7 +17,7 @@
<div class="market-values shrink-0">
<div class="size-h3 text-right {{ if eq .PercentChange 0.0 }}{{ else if gt .PercentChange 0.0 }}color-positive{{ else }}color-negative{{ end }}">{{ printf "%+.2f" .PercentChange }}%</div>
<div class="text-right">{{ .Currency }}{{ .Price | formatPrice }}</div>
<div class="text-right">{{ .Currency }}{{ .Price | formatPriceWithPrecision .PriceHint }}</div>
</div>
</div>
{{ end }}

View File

@ -79,6 +79,7 @@ type market struct {
Name string
Currency string
Price float64
PriceHint int
PercentChange float64
SvgChartPoints string
}
@ -106,6 +107,7 @@ type marketResponseJson struct {
RegularMarketPrice float64 `json:"regularMarketPrice"`
ChartPreviousClose float64 `json:"chartPreviousClose"`
ShortName string `json:"shortName"`
PriceHint int `json:"priceHint"`
} `json:"meta"`
Indicators struct {
Quote []struct {
@ -152,13 +154,14 @@ func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, erro
continue
}
prices := response.Chart.Result[0].Indicators.Quote[0].Close
result := &response.Chart.Result[0]
prices := result.Indicators.Quote[0].Close
if len(prices) > marketChartDays {
prices = prices[len(prices)-marketChartDays:]
}
previous := response.Chart.Result[0].Meta.RegularMarketPrice
previous := result.Meta.RegularMarketPrice
if len(prices) >= 2 && prices[len(prices)-2] != 0 {
previous = prices[len(prices)-2]
@ -166,21 +169,22 @@ func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, erro
points := svgPolylineCoordsFromYValues(100, 50, maybeCopySliceWithoutZeroValues(prices))
currency, exists := currencyToSymbol[strings.ToUpper(response.Chart.Result[0].Meta.Currency)]
currency, exists := currencyToSymbol[strings.ToUpper(result.Meta.Currency)]
if !exists {
currency = response.Chart.Result[0].Meta.Currency
currency = result.Meta.Currency
}
markets = append(markets, market{
marketRequest: marketRequests[i],
Price: response.Chart.Result[0].Meta.RegularMarketPrice,
Price: result.Meta.RegularMarketPrice,
Currency: currency,
PriceHint: result.Meta.PriceHint,
Name: ternary(marketRequests[i].CustomName == "",
response.Chart.Result[0].Meta.ShortName,
result.Meta.ShortName,
marketRequests[i].CustomName,
),
PercentChange: percentChange(
response.Chart.Result[0].Meta.RegularMarketPrice,
result.Meta.RegularMarketPrice,
previous,
),
SvgChartPoints: points,