Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published by Scroll Versions from space DEV and version r0821

D toc

Excerpt

Formats a numeric set of values according to the specified number formatting. Source values can be a literal value, a function returning a numeric value, or reference to a column containing Integer or Decimal values.

  • If the source value does not include a valid input for this function, a missing value is returned.
  • When a NUMFORMAT transform is applied, the column can be re-typed to a different data type. For example, if your format string (second parameter) is '#' (a single hash mark), then all values are rounded to the nearest integer, and the column is re-typed as Integer.

    Tip

    Tip: In general, you should format your numeric data after you have completed your computations on it. In some cases, you might lose numeric precision in converting formats, or your data can be re-typed to a different data type (e.g. Decimal to Integer).

  • For more information on formatting date values, see DATEFORMAT Function.

D s product
rtrue
supports a wide variety of number formats, following Java standards. For more information, please see Java's documentation.

Info

NOTE: This function just changes how the underlying cell value is displayed. If you round the value to a specific level of precision, please use the ROUND function. See ROUND Function.

D s lang vs sql

D s
snippetBasic

D lang syntax
RawWrangletrue
Typeref
showNotetrue
WrangleTextderive type:single value: numformat(MyPrice, '$###,###.##') as: 'dateformat_MyPrice'

numformat(MyPrice, '$###,###.##')

Output: Returns the values from the MyPrice column converted to a price format.

D s
snippetSyntax

D lang syntax
RawWrangletrue
Typesyntax
showNotetrue
WrangleTextderive type:single value:numformat(numeric_col, number_format_string)

numformat(numeric_val, number_format_string)


ArgumentRequired?Data TypeDescription
numeric_valYstring, integer, or decimalLiteral value, function returning a numeric value, or name of Integer or Decimal column whose values are to be formatted
number_format_stringYstringLiteral value of the number formatting string to apply

D s lang notes

numeric_val

Literal numeric value, a function that returns a numeric value, or the name of the column whose Integer or Decimal data is to be formatted.

  • Values with more than 20 digits after the decimal point are truncated by this function.

  • Missing values for this function in the source data result in missing values in the output.
  • Multiple columns and wildcards are not supported.

D s
snippetusage

Required?Data TypeExample Value
YesString (column reference), function, or Integer or Decimal literalMyPrice

number_format_string

String value indicating the number format to apply to the input values.

Info

NOTE: You cannot create number format strings in which a 0 value appears before a # value. The following example strings are not supported: #.#0, #.#0#, #.#00

 

Some key codes:

CodeDescriptionExample Format StringExample InputsExample Outputs
#Insert a digit if it is present in the data.

'###,###'

99

999

1000

10000

99

999

1,000

10,000

0Insert a digit even if it is not present in a data.'00.##'

20

7.1

20

07.1

$You can add constants values to the expression. Whitespace is respected.
For example, you can insert currency markers at the beginning of your expression.
'$ ##.##'20
2514.22
6.6666
$ 20
$ 2514.22
$ 6.67
%Percentage expressions can be at the back of the number formatting expression.'##.## %'20
2514.22
6.6666
20 %
2514.22 %
6.67%

D s product
supports Java number formatting strings, with some exceptions.

Missing values for this function in the source data result in missing values in the output.

D s
snippetusage

Required?Data TypeExample Value
YesString'#.#'


D s
snippetExamples

Example - formatting price and percentages

This example steps through how to manage number formatting for price and percentage data when you have to perform some computations on the data in the application.

Source:

In this case, you need to compute sub-total and totals columns.

OrderIdQtyUnitPriceDiscountTaxRate
10015$25.000%8.25%
100215$39.995%8.25%
10032$99.9915%8.25%
1004100$999.990%8.25%

Transformation:

When this data is first imported into the Transformer page, you might notice the following:

  • The data type for OrderId is an Integer, when it should be treated as String data.
  • The UnitPrice, Discount , and TaxRate columns are typed as String data because of the unit characters in the values.
Info

NOTE: Where possible, remove currency and three-digit separators from your numeric data prior to import.

You can re-type the OrderId column to String without issue. If you retype the other three columns, all values are mismatched. You can use the following transforms to remove the currency and percentage notation. The first transform removes the trailing % sign from every value across all columns using a

D s item
pattern
pattern
.

D trans
RawWrangletrue
p03Value''
Typestep
WrangleTextreplace col: * on: `\%{end}` with: ''
p01NameColumns
p01ValueAll
p02NameFind
p02Value`\%{end}`
p03NameReplace with
SearchTermReplace text or pattern

You can use a similar one to remove the $ sign at the beginning of values:

D trans
RawWrangletrue
p03Value''
Typestep
WrangleTextreplace col: * on: `{start}\$` with:''
p01NameColumns
p01ValueAll
p02NameFind
p02Value`{start}\$`
p03NameReplace with
SearchTermReplace text or pattern

When both are applied, you can see that the data types of each column is updated to a numeric type: Integer or Decimal. Now, you can perform the following computations:

D trans
RawWrangletrue
p03Value'SubTotal'
Typestep
WrangleTextderive type:single value:(Qty * UnitPrice) as:'SubTotal'
p01NameFormula type
p01ValueSingle row formula
p02NameFormula
p02Value(Qty * UnitPrice)
p03NameNew column name
SearchTermNew formula

You can use the new SubTotal column as the basis for computing the DiscountedTotal column, which factors in discounts:

D trans
RawWrangletrue
p03Value'DiscountedTotal'
Typestep
WrangleTextderive type:single value:(SubTotal - (SubTotal * (Discount / 100))) as:'DiscountedTotal'
p01NameFormula type
p01ValueSingle row formula
p02NameFormula
p02Value(SubTotal - (SubTotal * (Discount / 100)))
p03NameNew column name
SearchTermNew formula

The Total column applies the tax to the DiscountedTotal column:

D trans
RawWrangletrue
p03Value'Total'
Typestep
WrangleTextderive type:single value: DiscountedTotal * (1 + (TaxRate / 100)) as: 'Total'
p01NameFormula type
p01ValueSingle row formula
p02NameFormula
p02ValueDiscountedTotal * (1 + (TaxRate / 100))
p03NameNew column name
SearchTermNew formula

Because of the math operations that have been applied to the original data, your values might no longer look like dollar information. You can now apply price formatting to your columns. The following changes the number format for the SubTotal column:

D trans
RawWrangletrue
Typestep
WrangleTextset col:SubTotal value:numformat(SubTotal, '#.00')
p01NameColumns
p01ValueSubTotal
p02NameFormula
p02Valuenumformat(SubTotal, '#.00')
SearchTermEdit column with formula

Note that the leading $ was not added back to the data, which changes the data type to String. You can apply this transform to the Price, DiscountedTotal, and Total columns.

Info

NOTE: The data types for your columns should match the expected inputs for your downstream analytics system.

The Discount and TaxRate values should be converted to decimals. The following adjusts the Discount column:

D trans
RawWrangletrue
Typestep
WrangleTextset col:Discount value:(Discount / 100)
p01NameColumns
p01ValueDiscount
p02NameFormula
p02Value(Discount / 100)
SearchTermEdit column with formula

Results:

The output data should look like the following:

OrderIdQtyUnitPriceSubTotalDiscountDiscountedTotalTaxRateTotal
1001525.00125.000125.000.0825135.31
10021539.99599.850.05569.860.0825616.87
1003299.99199.980.15169.980.0825184.01
1004100999.9999999.00099999.000.0825108248.92
Noprint

See Also

Content by Label
showLabelsfalse
showSpacefalse
cqllabel = "math" and space = currentSpace()