Search code examples
xpageslotus-domino

xpages - Get rid of the fraction part of a currency


I'm trying to get rid of the fraction part of a field stored in a Notes document as a Number field. In xPages it displays default as a Double which I then want to convert to Currency. When I use the standard Currency converter I can't get rid of the fraction part.

I tried to convert the Double to display Integer Only and programmatically converted the Double to intValue. This works fine as long as I don't use the Currency converter which seem to add the fractions no matter what. I guess this is because the default fractionpart is 2.

I guess one important clue is that I'm talking about Swedish currency. In Sweden we display Currency in this pattern: # ### ### kr. With punctuation as a space and the currency symbol AFTER the number.

I tried to use Custom Converter - Pattern like the Above example but then I don't get the punctuation correct. So - what I'm trying to achieve is this: Example: 112 000 kr The Code Example will return this: 112 000,00 kr

Of course I can do a String Conversion-concatenation operation to achieve this but the best would be if there's a way to get a hold of the default fraction part for the CurrencyCode and manipulate that..? And to me it seems to be a bug because "Display Integer Only" seems to be disregarded.

Maybe there is a pattern conversion that I haven't found, that I can use?

Code Examples:

<xp:inputText value="#{document1.totSumCost}" id="totSumCost1">
        <xp:this.converter>
            <xp:convertNumber type="currency" currencySymbol="kr" currencyCode="SEK" integerOnly="true" maxFractionDigits="0">
            </xp:convertNumber>
        </xp:this.converter>
</xp:inputText>

I "sort of" solved this myself in this somewhat disturbing way:

<xp:inputText value="#{document1.totSumCost}"
                    id="inputText2">
                        <xp:this.converter>
                <xp:convertNumber type="currency" integerOnly="true"
                    locale="sv" maxFractionDigits="0" currencySymbol=" ">
                </xp:convertNumber>
            </xp:this.converter>
                </xp:inputText>
                <xp:text escape="true" id="computedField6">
                    <xp:this.value><![CDATA[#{javascript:return " kr"}]]></xp:this.value>
                </xp:text>

Solution

  • Use a custom converter:

    <xp:this.converter>
        <xp:customConverter>
            <xp:this.getAsString><![CDATA[#{javascript:
                value.toFixed(0).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1 ") + " kr"
                }]]></xp:this.getAsString>
            <xp:this.getAsObject><![CDATA[#{javascript:
                parseFloat(value.replace(" ", ""))
                }]]></xp:this.getAsObject>
        </xp:customConverter>
    </xp:this.converter>
    

    It converts numbers into Swedish currency format like
    123456789.12 --> 123 456 789 kr
    and parses it back to float.

    If you want to save value as integer then use parseInt() instead of parseFloat().