We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
1) Use NumberFormat.getCurrencyInstance() instead of getNumberInstance() - this automatically handles:
- Currency symbols
- Thousand separators
- Decimal formatting
2) Create proper Locale objects for each country:
- Locale.US for United States
- new Locale("en", "IN") for India (English language, India country)
- Locale.CHINA for China
- Locale.FRANCE for France
3) The getCurrencyInstance() method automatically formats numbers according to each locale's conventions, including:
- Correct currency symbols ($ for US, Rs. for India, ¥ for China, € for France)
- Proper grouping separators (commas, spaces, etc.)
- Decimal separators (periods or commas depending on locale)
This solution will produce the exact output shown in your sample, with proper formatting for each currency.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Currency Formatter
You are viewing a single comment's thread. Return to all comments →
The key changes:
1) Use NumberFormat.getCurrencyInstance() instead of getNumberInstance() - this automatically handles: - Currency symbols - Thousand separators - Decimal formatting
2) Create proper Locale objects for each country: - Locale.US for United States - new Locale("en", "IN") for India (English language, India country) - Locale.CHINA for China - Locale.FRANCE for France
3) The getCurrencyInstance() method automatically formats numbers according to each locale's conventions, including: - Correct currency symbols ($ for US, Rs. for India, ¥ for China, € for France) - Proper grouping separators (commas, spaces, etc.) - Decimal separators (periods or commas depending on locale)
This solution will produce the exact output shown in your sample, with proper formatting for each currency.