Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Currency object using a number

I got a service that's sending me the currency ID of the currency I should work with. The ID is a number that represents an ISO-4217 Currency.

The java Currency object enable to get an instance according to the currency string ID (i.e USD, JPY...), but not according to the ISO number code (i.e 840, 392...)

How can I get an instance using the number value?

like image 385
Shlomi Avatar asked Jan 27 '26 08:01

Shlomi


1 Answers

Maven artifact com.neovisionaries:nv-i18n:1.9 contains CurrencyCode, which is a Java enum listing all ISO 4217 codes. With CurrencyCode, getting a Currency instance from an ISO 4217 numeric code can be written like the following.

import java.util.Currency;
import com.neovisionaries.i18n.CurrencyCode;

public class Test
{
    public static void main(String[] args)
    {
        // Get the Currency instance for 392 (Japanese Yen).
        Currency currency
            = CurrencyCode.getByCode(392).getCurrency();

        // This prints "JPY".
        System.out.println("currency = " + currency);
    }
}

nv-i18n project page:
https://github.com/TakahikoKawasaki/nv-i18n

Maven:

<dependency>
    <groupId>com.neovisionaries</groupId>
    <artifactId>nv-i18n</artifactId>
    <version>1.9</version>
</dependency>
like image 53
Takahiko Kawasaki Avatar answered Jan 29 '26 20:01

Takahiko Kawasaki