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.
Java Currency Formatter
Java Currency Formatter
Sort by
recency
|
899 Discussions
|
Please Login in order to post a comment
import java.util.; import java.text.;
public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double payment = scanner.nextDouble(); scanner.close();
}
in java 15
NumberFormat.getCurrencyInstance(new Locale("en","IN")).format(payment); --> this now returns the rubis char , it's been updated, the expected output of the exercice is Rs. , not the rubis icon, thats why i cannot pass the test cases
expected output should be updated.
public class Solution {
}
I managed to adapt the solution for Java 15 and I got the correct output, but the test fails either way 😞 I double checked it with diff and it's 100% the same, including types of spaces (narrow non-breaking space vs. regular space for French formatting). I spent a lot of time investigating on how to get the correct response with
NumberFormat, so I share the solution.The idea is based on using the
DecimalFormatderived class that alllows for cusomization of many low-level aspects of the currency rendering throughDecimalFormatSymbolssubstitution – these include the currency symbol and grouping separator that have changed in the locales between Java 8 and Java 15. The second one obviously doesn't work again, so I have to apply the substitution on the final string too, which I'm not happy with, of course. But in general it looks for me like a good answer, with one quirk. What do you think?Here's the code: