36 lines
796 B
Go
Raw Normal View History

2018-04-18 12:01:51 +00:00
package utils
import (
"fmt"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-04-18 12:01:51 +00:00
"github.com/pariz/gountries"
)
// Current currency rates
const (
2018-06-30 04:06:33 +00:00
yenToEuro = 0.0077
yenToDollar = 0.0090
2018-04-18 12:01:51 +00:00
)
var countryQuery = gountries.New()
// YenToUserCurrency converts the Yen price to the user currency.
func YenToUserCurrency(amount int, user *arn.User) string {
if user == nil || user.Location.CountryName == "" {
return fmt.Sprintf("%.2f $", float64(amount)*yenToDollar)
}
country, err := countryQuery.FindCountryByName(user.Location.CountryName)
if err != nil {
return fmt.Sprintf("%.2f $", float64(amount)*yenToDollar)
}
if arn.Contains(country.Currencies, "EUR") {
return fmt.Sprintf("%.2f €", float64(amount)*yenToEuro)
}
return fmt.Sprintf("%.2f $", float64(amount)*yenToDollar)
}