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}
Related
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.
I'm a beginner in Java, and NetBeans. I'm trying to make a simple program where you introduce 2 numbers and their sum gets divided by two. However, I'm using JFormattedTExtFields and I don't know how to customize the allowed input in them. Basically I'm trying to find out how to:
Only allow numbers to be entered in JFormmatedTextField;
Only allow a certain amount of numbers;
You could use a NumberFormat and specify the maximum number of integer digits with setMaximumIntegerDigits.
Here's a nice article.
Basically you can do something like:
NumberFormat f = NumberFormat.getNumberInstance();
f.setMaximumIntegerDigits(maxDigitsAmount);
JFormattedTextField field = new JFormattedTextField(f);
The Format should guarantee that the inserted String satisfy the format. Anyway even if a number is supplied, the textfield will store it as a String. So if you need your original Integer you need to rebuild it like suggested #noise:
Integer i = Integer.toString(field.getText());
I am aware of the various posts floating out there with regards to the same issue.
Mine its a little bit different and it might be a little obvious, but I will need your comments.
I am currently using Hibernate Search and Lucene to Index entity properties.
I have a bunch of Double properties on my entities.
These entities using the default Bridges from Lucene (Bridge i.e the one in charge converting LongToString and StringToLong) are giving me troubles once the scientific notation starts to be used.
I am trying to show on DataTables on a .xhtml Credit and Debit amounts, their lenght can be as long as 18 digits, and their DataBase (DB2) type is BIGINT.
I can not change the DataBase type
to Long for example.
I can not change either the Double
type attributes of my entities
either to for example Long
So whats the question?
Is there a way from a String say "1234567890" to retrieve a Double whose format is 1234567890 and not 1.23456789E9 as it is being done by default by Double.parseDouble(FormattedString)?
PD: I am aware of the existance of DecimalFormat, however take into account using this formater will give me a String formated correctly say : "#######.E0" but what I really need is a Double with such format, however when doing Double.parseDouble(FormattedString) I will loose such format.
Hope I was clear and thanks for any help.
Is there a way from a String say "1234567890" to retrieve a Double whose value is 1234567890 and not 1.23456789E9 as it is being done by default by Double.parseDouble(FormattedString)?
Your question doesn't really make sense. 1234567890 is the same value as 1.23456789E9 and a double represents one of them, if and only if it also represents the other.
I am aware of the existance of DecimalFormat, however take into account using this formater will give me a String formated correctly say : "#######.E0" but what I really need is a Double with such format, however when doing Double.parseDouble(FormatedString) I will loose such format.
No, there is no way to construct a Double so that it is displayed in a certain way. The toString method for Double is what it is, and it can't be changed.
The only thing you can do is to for instance use DecimalFormat or String.format but as you've noted, you'll always end up with a String.
Don't know nothing of Lucene, but you can never have a Double in a .xhtml Document it is always a characterstring. A Double doesn't have a Format, only a String representation of a Double has.
So I finally got the solution to my problem.
After rounding up what aioobe and Jens Schauder said. I am able to format the text dynamically on my .xhtml with the following tag:
<h:outputText value="#{recordTable[column.property]}"
rendered="#{column.header ne 'Details' and
column.header eq ('Total Credit Amount' or
'Total Debit Amount')}">
<f:convertNumber pattern="########"/>
</h:outputText>
Thanks for making clear to me these basic stuff I had blurred :)
I am working on a project that requires some simple math to be performed on currency, however it arrives in the form of a String. I am new to Java/Android so I am looking for help in converting from a String to a data type appropriate to this operation. At first I thought Float was right but after reading elsewhere and introducing myself to the numbers class, it appears BigDecimal is correct. Am I on the right track? At this point I simply want to subtract the sum of payments from an initial invoice amount. I get the feeling this code is simple but clumsy and I suspect I am missing a great deal about the nuances of working with currency. How would you do it? All advice warmly appreciated!
// capture variables from sending activity
String invoiceAmt = getIntent().getStringExtra("invoiceAmt");
String paymentsSum = getIntent().getStringExtra("paymentsSum");
// convert strings to BigD's
BigDecimal amt = new BigDecimal(invoiceAmt);
BigDecimal sum = new BigDecimal(paymentsSum);
// Do the math
BigDecimal invDue = amt.subtract(sum);
// insert the value (back to string) into textView
TextView tvInvoiceDue = (TextView)findViewById(R.id.InvoiceDue);
tvInvoiceDue.setText(invDue.toString());
BigDecimal is a fine approach. So is using an int or a long to store cents. I've heard some people like Joda-Money but I've never used it myself.
See this question.
In the code you posted, make sure the Strings you are receiving don't have currency symbols in them.
I am trying to store the 10 digit mobile number in int, but it is saying that its out of range, how can i store in int, any help pls
There is no semantic value in storing it as a number - you won't be doing any numerical operations on it. You should store the value as a string.
A mobile number might start with a 0 (zero) therefore I wouldn't advice to store it as a number. My advice would be to store it as a String. Otherwise you should use a long.
What about a proper TelephoneNumber type class:
public interface TelephoneNumber
A simple re-usable entity class that defines attributes of a telephone number.
You'd better use String, but you can also use long or BigInteger.
But using String will allow you to store dashes or spaces between numbers.
Mobile numbers uses and E.164 numbering format for telephone or digital communications. You will have to use Strings as valid characters such as "+"/"-" can be found in mobile numbers.
Use a String, for for proper semantics I would wrap it in your own MobileNumber class.
Did you have a look at Java's BigInteger? Two possible references are:
Oracle BigInteger
Java Notes BigInteger
use Long or String instead of Integer. Or give us some code examples for what you are trying to do.