Converting String to Money - java

I am having the below String
String money = "USD0.00"
Want to convert this into org.joda.money.Money
Do I need to split the currency unit and double value here and need to set it as separately in Money. Or Any library api is there to convert this?

You can use Money.parse().
From the documentation (emphasis mine)
The string format is '$currencyCode $amount' where there may be zero to many spaces between the two parts.

Related

Formatting Float Value to specific Format - Java vs C# Number Formatting

I need to convert Byte to KB.So i divide the value by 1024
I need to display the value in this format specified originally in Java Number formatting ###,###,###,##0.00 KB
This code
string format="###,###,###,##0.00 KB";
return String.Format(format, x);
produces the following output
###,###,###,##0.00 KB
This formatting string is specified in the Java counterpart,wont the same approach work in C#?
Please advice.
String.Format and IFormattable.ToString (the formatting you need here) are different, yet related things.
String.Format requires some format string with placeholders and the substituted values can also have formatting if they implement the IFormattable interface.
Console.WriteLine(String.Format("{0} KB", 42.ToString("###,###,###,##0.00")));
The formatting of 42 can be inlined:
Console.WriteLine(String.Format("{0:###,###,###,##0.00} KB", 42));
Which can be simplified further by interpoation:
Console.WriteLine($"{42:###,###,###,##0.00} KB"));
Of course, 42 can be a variable in the interpolation ($"{numValue:###,###,###,##0.00} KB}"). However, the format string cannot be a variable, so this will not work:
string format = "{x} KB";
Console.WriteLine($format); // does not compile, use String.Format in this case
Remark:
Console.WriteLine also supports formatting so the examples above could have been written like this:
Console.WriteLine("{0:###,###,###,##0.00} KB", 42);
I used explicit String.Format just to avoid confusion.
Update
If the size formatting comes from an external source you cannot inline it into the format string but this is not a problem. So if you have
string fileSizeFormat = "###,###,###,##0.00 KB";
You can still use myFloatWithFileSize.ToString(fileSizeFormat). In this case String.Format is needed only if you want to embed this into a nice sentence or something:
return String.Format("The size of the file: {0}", fileSize.ToString(fileSizeFormat));
or with interpolation:
return $"The size of the file: {fileSize.ToString(fileSizeFormat)}";

String with EX, EEX, EXP or EE scientific notation to double

In my code I am receiving numeric values as strings from different sources. One of the source is sending me this kind of value:
-6.535402781EX-05
After few tests I know that EX format is not handled by Double.valueOf() method. NumberFormatException is thrown.
I figured out easy workaround:
String val = "-6.535402781EX-05".replace("X", "");
Actually it is working, but I am not sure if that's best solution. I saw also EEX, EE and EXP. Question: How to protect my code for this kind of edge cases?
You may want to use replaceAll witha regex instead of replace if those are the only possible values:
String[] val = {"-6.535402781EX-05","-6.535402781EEX-05","-6.535402781EE-05","-6.535402781EXP-05"};
for(String v :val){
System.out.println(v.replaceAll("[EXP]{2,}", "E"));
}
Would a simple regular expression do the trick for you? You could first convert the different input formats to your known input format that can be handled by Double.valueOf().
String pattern = "(\\D?)(\\d+)(\\.?)(\\d+)(\\D+)(\\d+)";
List<String> inputs = Arrays.asList("-6.535402781EX-05",
"-6.535402781EXP-05",
"-6.535402781EE-05",
"-6.535402781E-05",
"6.535402781E-05",
"6.535402781",
"-6.535402781",
"6.5",
"6");
inputs.forEach((String in) -> System.out.println(in.replaceAll(pattern, "$1$2$3$4E-$6")));
The inputs in my example should be converted to the following and parsing them should be possible with Double.valueOf().
-6.535402781E-05
-6.535402781E-05
-6.535402781E-05
-6.535402781E-05
6.535402781E-05
6.535402781
-6.535402781
6.5
6
You should be careful to add enough unit test cases for all input formats you want to support.

Texfield contain integer, String or double value in java

How to check the datatype of entered value in TextField? I have found other questions related to this query at Stack Overflow, but those are related to other languages, as: C#, Swift etc.
I don't want to parse the value of String to integer, or vise versa. I want to identify the Datatype of the value which is contained by jTextField1.
Usually in java, jTextField1.getText(); returns the String value in Java.
What I want is:
if jTextField1 contains Hello World then answer should be String
if jTextField1 contains 123 then answer should be integer
if jTextField1 contains 354.55 then answer should be Double
Is there any way to do this?
General - the datatype of a field is always String....
Integer.parse or Double.parse would be the most useful tools here, but if you dont want to use it, you can check each character individually if its a number Character.isDigit.
If any is not: String
If all are and no separator --> int
else --> double
but as said - I would use the existing api methods - because at the end, parsing is parsing

How display currency with Freemarker when using Longs

So let's say I have:
class Person
{
private long salary;
}
Where 5345273 would be equal to $53,452.73 (in other words the last two digits are the cents.
Is there a way to directly reference ${Person.salary} so that it displays the proper amount?
I did see that there are pre-defined formats such as Currency but there doesn't appear to be one if you use longs. I did see it was possible to create your own custom formatter but is this the only way? And if so is there anyone who already created one because I can't imagine I'm the only person using long to manage currency amounts. And also is that the correct solution?
Should be possible to convert the long to a double in the expression. Something like this: ${(Person.salary/100)?string.currency}

Inserting custom String between number and decimal - Java DecimalFormat

Hello and thank you in advance for the help.
I am having some trouble formatting using a Java function to mark up a price in HTML.
It seems that, no matter what I do, I cannot insert custom content between the numbers and the decimal (throws Illegal Argument Exception). Is there any known way to achieve the following:
NumberFormat nf = getNumberFormat("'<span class=\"dollars\">'##'</span></span class=\"decimal\">'.'</span></span class=\"cents\">'00'</span>'", locale);
nf.format(number);
Assume locale and number are correctly initialized.
If you look at the docs for DecimalFormat you'll see that they talk about the prefix and the suffix text - but not putting arbitrary text within a number.
It sounds like you should basically write this bit of formatting yourself - possibly using DecimalFormat for each section of the number.
You might consider using String.format(String pattern, Object... arguments). You can pass your simply formatted numbers as arguments.

Categories