Archived
1
0
Fork 0

Add USD currency to price format on Twitter

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2021-02-28 15:33:59 +01:00
parent 2afd36584b
commit 0902b13705
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7
2 changed files with 31 additions and 5 deletions

View file

@ -90,16 +90,18 @@ func (c *TwitterNotifier) buildHashtags(productName string) string {
return "" return ""
} }
// replace price currency by its symbol // formatPrice using internationalization rules
// euro sign is placed after the value
// default the currency, or symbol if applicable, is placed before the value
func formatPrice(value float64, currency string) string { func formatPrice(value float64, currency string) string {
var symbol string
switch { switch {
case currency == "EUR": case currency == "EUR":
symbol = "€" return fmt.Sprintf("%.2f€", value)
case currency == "USD":
return fmt.Sprintf("$%.2f", value)
default: default:
symbol = currency return fmt.Sprintf("%s%.2f", currency, value)
} }
return fmt.Sprintf("%.2f%s", value, symbol)
} }
// NotifyWhenAvailable create a Twitter status for announcing that a product is available // NotifyWhenAvailable create a Twitter status for announcing that a product is available

View file

@ -55,3 +55,27 @@ func TestBuildHashtags(t *testing.T) {
}) })
} }
} }
func TestFormatPrice(t *testing.T) {
tests := []struct {
value float64
currency string
expected string
}{
{999.99, "EUR", "999.99€"},
{999.99, "USD", "$999.99"},
{999.99, "CHF", "CHF999.99"},
{999.99, "", "999.99"},
}
for i, tc := range tests {
t.Run(fmt.Sprintf("TestFormatPrice#%d", i), func(t *testing.T) {
got := formatPrice(tc.value, tc.currency)
if got != tc.expected {
t.Errorf("for value %0.2f and currency %s, got %s, want %s", tc.value, tc.currency, got, tc.expected)
} else {
t.Logf("for value %0.2f and currency %s, got %s, want %s", tc.value, tc.currency, got, tc.expected)
}
})
}
}