1
votes

I have a Xpage with three fields, all Number declared; Nominal, Price and PaymentAmount. I want to calculate the PaymentAmount using Nominal * Price.

In SSJS onChange I use the following code:

var price = getComponent("Price").getValue();

to get the value from the field "Price".

In Sweden we enter our numeric values as this #.###,## 1.234,56

If I enter the values Nominal=10 and Price=2,5 in my Xpage and try to calculate using the above mentioned formula the value stored in var "price" is converted to 25 and of type long.

Please advice

/M

3
If the Editbox is set to any type of "number" (Number, Currency…) getValue() will return a long if the value if the field is 2,5. It will convert it and remove the fraction as 2,5 -> 25Mikael Andersson Wigander

3 Answers

2
votes

I'm an idiot, unfortunatly.

Did this:

<inputText value="#{document1.Price}" id="Price" required="true" size="10">
    <this.validators>
        <validateRequired message="Price is required">
        </validateRequired>
    </this.validators>
    <this.converter>
        <convertNumber type="number" locale="sv">
        </convertNumber>**
    </this.converter>
</inputText>

and it works!

Thanks you ALL for input

0
votes

I take it "Price" is an edit box? If so you can set it to Number field, then Display format to currency. The currency code will define how that number is displayed.

enter image description here

When you get the components value it should come back as a normal number you can work with.

0
votes

getComponent gets the UI component value and that's why it does not necessarily reflect the data type that will be saved (in UI everything is string). It's also a slow way to get the value.

Try datasourcename.getItemValueDouble("itemname") instead. Note that you need to use the field name on Form, not the component name.

Regarding number format make sure your djConfig locale is Sweden, probably se-se. For me in Finland it looks like this:

<script type="text/javascript" src="/xsp/.ibmxspres/dojoroot-1.6.1/dojo/dojo.js" djConfig="locale: 'fi-fi', parseOnLoad: true"></script>

It should use the browser locale. To force this in SSJS you can do:

context.setLocaleString("se-SE");

in beforePageLoad event.