1
votes

In my form I have four fields that should be calculated and the result displayed in the last field called PaymentAmount. I was recommended to use a custom converter for PaymentAmount because I then only needed to have partial refresh on PaymentAmount for onchange event on the other four fields.

This works very good but my problem is that the result is in wrong format.

My code looks like this:

getAsObject()

try {
    var transCode = getComponent("TransactionCode").getValue();
    var nominal = getComponent("Nominal").getValue();
    var price = getComponent("Price").getValue();
    var qFactor =  getComponent("QuoteFactor").getValue()||1;
    var fee1= getComponent("Fee1").getValue()||0;
    var fee2= getComponent("Fee2").getValue()||0;
    var fee3= getComponent("Fee3").getValue()||0;
    var feeTotal = fee1+fee2+fee3;

    var paymentAmount = nominal * (price * qFactor);
    if(transCode == "Buy") {
        paymentAmount+=feeTotal;
    } else if(transCode == "Sell") {
        paymentAmount -= feeTotal;
    } else return 0;

    return paymentAmount;
} catch(e){
    dBar.error(e);
}

getAsString()

return value.toString();

I have tried to format the result using all available methods and objects in java like: String.format("%.2f", value); but failed.

If I enter my values according to my locale 10000*1,44+1,2 = 14401,2 but the result displayed in PaymentAmount is 14401.2. I'd like it to be displayed according to my locale 14001,2.

If I for instance use the following in getAsString() I get this error:

try {
    var val = java.lang.String.format("%.2f",value);

} catch(e) { dBar.error(e); }

com.ibm.jscript.InterpretException: Script interpreter error, line=2, col=28: Java method 'format(string, number)' on java class 'java.lang.String' not found

I can't get the correct data type for value in getAsString().

For you who has seen/commented on my earlier questions I'm stuck with these "localisation issues" once again...

Please advice

/M

1

1 Answers

2
votes

You can use this Java code to get the number in your locale:

import java.text.NumberFormat;
import java.util.Locale;
...
...
Locale swedishLocale = new Locale("sv", "SE"); // Locale for Sweden
NumberFormat nf = NumberFormat.getInstance(swedishLocale);
return nf.format(14401.2);

This would return 14 401,2. Do not try convert this into SSJS otherwise you would get an error of Ambiguity when calling format(long) and format(double). I have bitten by this before.

Update

You can create a class with a static method which formats the date in your locale.

package pkg;
import java.text.NumberFormat;
import java.util.Locale;
public class Utilities {
    public static String formatString() {
        Locale swedishLocale = new Locale("sv", "SE");
        NumberFormat nf = NumberFormat.getInstance(swedishLocale);
        return nf.format(14401.2);
    }
}

Then you can call this method in SSJS by:

var formattedNumber = pkg.Utilities.formatString();

You can add parameters to method as per your requirements.