Parse and Format Decimal Function Calls

Last modified: February 13, 2024

1 Introduction

This document describes parsing and formatting decimal function calls. For details on all the pattern possibilities, see Class DecimalFormat.

2 parseDecimal

Parses a string value to a decimal value according to a specified format.

2.1 Input Parameters

The functionality of parseDecimal() depends on whether it is used in a microflow or a nanoflow.

2.1.1 Input Parameters in Microflows

The input parameters in microflows are described in the table below:

Value Type
Value to parse String
Format for the input value based on the Java library DecimalFormat (for more information, see Class DecimalFormat) String
Default value (optional) Decimal or empty

2.1.2 Input Parameters in Nanoflows

The input parameters in nanoflows are described in the table below:

Value Type
Value to parse String
Default value (optional) Decimal or empty

2.2 Output

The output is described in the table below:

Value Type
The output is a decimal value that matches the supplied string value. If the value cannot be parsed (meaning, it does not match the format parameter or contains illegal characters), the default value will be returned. If no default value is provided, an error occurs. Decimal

2.3 Examples

The following examples apply to both microflows and nanoflows. They demonstrate which output you get depending on input parameters.

  • parseDecimal('3.45') returns 3.45
  • parseDecimal('noDecimal', 5.05) returns 5.05
  • parseDecimal('noDecimal', empty) returns empty

Given a variable that is empty:

  • parseDecimal($StringVariable) will throw an error
  • parseDecimal($StringVariable, empty) returns an empty; the resulting variable may throw an error when used as a decimal (like in the expression '$var > 0')
  • parseDecimal($StringVariable, 0) returns 0; the resulting variable will always be a decimal and never throw an exception

The following example only applies to microflows, given that in nanoflows, you cannot manually specify the format:

  • parseDecimal('3,241.98', '#,###.##') returns 3241.98

3 formatDecimal

Converts a decimal value to a string value according to a specified format.

3.1 Input Parameters

The functionality of formatDecimal() depends on whether it is used in a microflow or a nanoflow.

3.1.1 Input Parameters in Microflows

The input parameters in microflows are described in the table below:

Value Type
Value to convert Decimal
Format for the result based on the Java library DecimalFormat (for details, see Class DecimalFormat) String
Locale in which the results should be formatted (optional). For the more information on supported values, see forLanguageTag. When omitted, the user configured locale is used. String

3.1.2 Input Parameters in Nanoflows

In nanoflows, this function only takes a single parameter described below:

Value Type
Value to convert Decimal

3.2 Output

The output is described in the table below:

Value Type
A string representation of the decimal in the format specified by the format parameter. String

Note that the exact output may depend on the Java version and user locale, depending on the passed format.

3.3 Microflow Examples

The examples below illustrate which value the expression returns:

  • If you use the following input:

    1
    
    formatDecimal(1234.56, '#,###.#')
    

    the output is (depending on the language settings):

    1
    
    '1,234.5' or '1.234,5'
    
  • If you use the following input:

    1
    
    formatDecimal(1234.56,  #,##0.00')
    

    the output is (depending on language settings):

    1
    
    '€ 1.234,50' or '$ 1,234.50'
    
  • If you use the following input:

    1
    
    formatDecimal(0.56, '% ##0')
    

    the output is

    1
    
    '% 56' 
    

3.4 Nanoflow Examples

In a nanoflow, this will format the decimal using the format appropriate to the locale from the language of the current user in Mendix apps.