So far I haven't found a solution to my problem.
I want to change a value.
So can convert value from $5,000,000 to 5000000 in java/kotlin?
So far I haven't found a solution to my problem.
I want to change a value.
So can convert value from $5,000,000 to 5000000 in java/kotlin?
My "personal" approach is to try and figure out how you got the input in the in the first place. In this case, from Java, you'd probably use a NumberFormatter
to format a numerical value to a String
, so you might want to consider reversing it.
String text = "$5,000,000";
NumberFormat formatter = NumberFormat.getCurrencyInstance();
Number number = formatter.parse(text);
System.out.println(number.intValue());
Will print 5000000
Now, this will assume that the Locale
the JVM is using makes use of $
and not say, pounds or yen, but you can seed the Locale
when you call NumberFormat.getCurrencyInstance()
, so that's one level of customisation you have.
Of course, you could just drop back to "$5,000,000".replace("$", "").replace(",", "")
and then parse the result to an int
, but I'm not a massive fan of it, but I'm peculiar that way