Numbers To and From Strings

Modified on Thu, Nov 30, 2023 at 1:44 PM

Numbers and Strings

Converting Numbers from and to Strings

 A "String" is text and cannot be used in arithmetic calculations. A number must be converted into a String before it can be used in String operations. For example, if you wish to express a number as a percentage (e.g. 34.5%), before the % sign can be concatenated with the value, the number must be converted to a String.

The functions listed on this page perform these conversions.

Converting from Strings to Numbers

There are three different types of numbers supported in the ReportWriter and each type has a different conversion function:

  • Integer or Whole Numbers - To convert from a String to an Integer, use the 'toInteger' function: ${rw:toInteger(params.myIntegerParameter)} where 'params.myIntegerParameter' can be any column or parameter. There is also a rw:convertIfPossibleToInteger function. This attempts to convert complicated Strings that begin with numbers to an Integer. It is designed to decipher complicated Strings of Print Periods into a simple Integer.
  • Double or Decimal Number - To convert from a String to a decimal number, use the 'toDouble' function: ${rw:toDouble(row.myNumber)} where 'row.myNumber' can be any column or parameter.
  • BigDecimal or real arithmetic Number - BigDecimals are a special type of number that is used when complicated arithmetic calculations must be performed and the result must be extremely accurate. BigDecimals are better than Doubles for precise arithmetic calculations (they are a more accurate way of representing decimal numbers). To convert to a BigDecimal, use the toBigDecimal function: ${rw:toBigDecimal(params.myString)}


Converting Numbers to Strings 

A 'formatNumber' function is provided to convert a number into exactly the format you need in order to print it out. Formatting typically controls how many digits and decimal places a printed number will have. Frequently, after a numeric calculation, a number could be represented with many decimal places (e.g. 43.29411). Formatting allows you to control the number of decimal places that are actually printed (e.g. 43.29).

Once a number has been converted to a String, it can be concatenated with other Strings:

 ${rw:formatNumber(row.myBigDecimal,"##.00")}% - In this example the '%' falls outside the ${}expression and is just 'literal text'.  Another way of doing this more explicitly and entirely within the Expression, using the 'concat' function, is:

${ rw:concat(rw:formatNumber(row.myBigDecimal,"##.00"),'%')}

The formatNumber function:

  • formatNumber(number,format) - The "number" can be any column (e.g. row.mynumber) that contains a number. The "format" is a pattern which tells the formatNumber function HOW to format the number.




In a number format, a "#" stands for a digit and a "0" stands for a decimal place. 

A "." is the decimal point: "##.00" will give you a number with at most two decimal places and two digits to the left of the decimal place.

Formats are quite literal. They restrict and specify the number of digits you can have to the right and left of the decimal point.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article