API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Ambiguous Error Formatting A Number
I was doing some formatting of a number (a dollar amount here in the US) and was getting an error about an ambiguous number. Here was the code I was trying to use:

var db:NotesDatabase = session.getCurrentDatabase();
var vw:NotesView = db.getView("vwByUser");
var key:string = userBean.commonName;
var doc:NotesDocument = vw.getDocumentByKey(key);
var bank:double = doc.getItemValueDouble("CurrentBank");
var fmt:java.text.NumberFormat = java.text.NumberFormat.getInstance(Locale.US);
fmt.setCurrency(java.util.Currency.getInstance(Locale.US));
fmt.setMinimumFractionDigits(2);
fmt.setMaximumFractionDigits(2);
var temp:string = fmt.format(bank);
if (temp.indexOf(".00") == -1) {
     return temp;
} else {
     return temp.substring(0, temp.indexOf(".00"));
}


Don't worry about the first half of the code - it just finds the current user's document in a view sorted by user name and pulls out their current bank account amount. The value is stored in the variable "bank".

The problem I ran into happens when setting the string variable temp. The problem happens if the value in the document was a whole dollar amount. Even though I told the code "getItemValueDouble" and the variable "bank" is defined as a double, it was showing up as an integer and when I tried to format that integer is when I got the ambiguous error.

Using a tip I found on Stack Overflow, I changed the code to this:

var db:NotesDatabase = session.getCurrentDatabase();
var vw:NotesView = db.getView("vwByUser");
var key:string = userBean.commonName;
var doc:NotesDocument = vw.getDocumentByKey(key);
var bank:double = doc.getItemValueDouble("CurrentBank");
var bankAmt:java.lang.Double = new java.lang.Double(bank);
var fmt:java.text.NumberFormat = java.text.NumberFormat.getInstance(Locale.US);
fmt.setCurrency(java.util.Currency.getInstance(Locale.US));
fmt.setMinimumFractionDigits(2);
fmt.setMaximumFractionDigits(2);
var temp:string = fmt.format(bankAmt);
if (temp.indexOf(".00") == -1) {
     return temp;
} else {
     return temp.substring(0, temp.indexOf(".00"));
}

(Changed code appears in bold). This time, I took the value from the document and put it into a java.lang.Double object. This is forcing the value to be a double even if it started out as an integer. Then, formatting that new object doesn't trigger an error.

The last few lines of code return the value either as a whole number (no decimal places) or exactly 2 decimal places. Since the Local is the Unites Sates ("Locale.US") with my few users being entirely US-based, I could rely on the US formatting of a decimal point between the whole number and the fractional digits.

If I wanted to show either a whole number or exactly 2 fraction digits in some other (unknown at compile time) location, I would handle it by seeing if the last two characters of the "temp" string are "00" and then, if so, remove the last three characters - that would remove the separator between the whole number and the fraction digits if the fraction digits are "00".