Working with Decimal types in Soma
This article describes how to convert fields into decimal types in Algoreus and perform transformations on them.
Before you begin
This article presumes that you have set up a Algoreus node connection to a database, file system, or another supported storage system that contains your data.
Reading decimal type data
Open an object in Algoreus.
In the case of a database or a BigQuery connection, if the table has a decimal column, Algoreus automatically converts it into a BigDecimal type.
When you create an axon from Algoreus, such a column will automatically get mapped to the Algoreus decimal type.
On the other hand, if your dataset contains non-decimal data that you want to convert into a decimal type, you can do it using the set-column directive as shown below:
set-column :decimal_column exp:{new("java.math.BigDecimal", <input column name>)}
Once this directive is executed, the column’s data type changes to BigDecimal, and similar to the previous steps, the schema also contains the appropriate data type.
Note: The <input column> can be of type String, Integer, Long, Float, or Double.
If your dataset includes values with varying scale, such as 1.05, 2.698, 5.8745512, you need to set the scale with Algoreus directive and also edit the schema in the axon to set the scale for the decimal column. To set the scale in the Algoreus, use a directive similar to the following:
set-column :ouput_col exp:{new("java.math.BigDecimal", decimal_col).setScale()}
For example, to convert a column of string called cost to decimal with a scale of 9 and output the results to a new column called output_col, use the following directive:
set-column :output_col exp:{new("java.math.BigDecimal", "cost").setScale(9)}
Transforming decimal data
Since the underlying data type for decimal columns in Algoreus is the Java BigDecimal class, you can use methods of the BigDecimal class to transform these columns, once they are converted into BigDecimal. In all the following directives, decimal_col is the decimal column that will be transformed, while output_column is the output of the operation
Transformation Example Code Snippets
Get the absolute value
set-column :output_col decimal_col.abs()
Get the precision of a decimal value
set-column :output_col decimal_col.precision()
Get the scale of a decimal value
set-column :output_col decimal_col.scale()
Get the unscaled value of a decimal value
set-column :output_col decimal_col.unscaledValue()
Add two decimal columns
set-column :output_col decimal_col.add(another_col)
Subtract a decimal from another
set-column :output_col decimal_col.subtract(another_col)
Multiply a decimal with another
set-column :output_col decimal_col.multiply(another_col)
Divide a decimal column by another and return the quotient
set-column :output_col decimal_col.divide(another_col)
Divide a decimal column by another and return the remainder
set-column :output_col decimal_col.remainder(another_col)
Convert decimal to a integer
set-column :output_col decimal_col.intValue()
Convert decimal to a long
set-column :output_col decimal_col.longValue()
Convert decimal to a float
set-column :output_col decimal_col.floatValue()
Convert decimal to a double
set-column :output_col decimal_col.doubleValue()
Check if a decimal value is equal to another
set-column :output_col decimal_col.equals(another_col)
Find the maximum of two decimal columns
set-column :output_col decimal_col.max(another_col)
Find the minimum of two decimal columns
set-column :output_col decimal_col.min(another_col)
Move the decimal point n places to the left
set-column :output_col decimal_col.movePointLeft(n)
Move the decimal point n places to the right
set-column :output_col decimal_col.movePointRight(n)
Get the nth power of a decimal
set-column :output_col decimal_col.pow(n)
Negate a decimal
set-column :output_col decimal_col.negate()
Strip trailing zeros in a decimal
set-column :output_col decimal_col.stripTrailingZeros()
Last updated