Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us to spark ideas for how to put the latest capabilities into action. Register here!
cancel
Showing results for 
Search instead for 
Did you mean: 
RVeitch_84
Creator
Creator

Convert a string to Big Decimal

I have the following string in a csv delimeted file that I need to convert to a BigDecimal :

"$6.00"

Thanks again

Labels (5)
1 Solution

Accepted Solutions
Jamesclary
Contributor II
Contributor II

You can use the `BigDecimal` class in Java to convert the string "$6.00" to a `BigDecimal` object. Here's an example code snippet that shows how to do this:

String str = "$6.00";

BigDecimal bd = new BigDecimal(str.replace("$", ""));

Explanation:

We start by initializing a string variable `str` with the value "$6.00".

We then create a `BigDecimal` object `bd` by passing the string `str` to the constructor of `BigDecimal`.

However, since the string `str` contains a currency symbol "$" that is not valid in a `BigDecimal` object, we remove it using the `replace()` method. The resulting string "6.00" can then be converted to a `BigDecimal` object using its constructor.

Note that you may also need to handle any potential exceptions that can be thrown by the `BigDecimal` constructor, such as `NumberFormatException` if the input string is not a valid decimal number. SkyWard Alpine Login

 

 

View solution in original post

2 Replies
Anonymous
Not applicable

try this expression on tJavaRow

output_row.data = new java.math.BigDecimal(input_row.data.replace("$",""));

 

or on tMap:

new java.math.BigDecimal(row1.data.replace("$",""))

Jamesclary
Contributor II
Contributor II

You can use the `BigDecimal` class in Java to convert the string "$6.00" to a `BigDecimal` object. Here's an example code snippet that shows how to do this:

String str = "$6.00";

BigDecimal bd = new BigDecimal(str.replace("$", ""));

Explanation:

We start by initializing a string variable `str` with the value "$6.00".

We then create a `BigDecimal` object `bd` by passing the string `str` to the constructor of `BigDecimal`.

However, since the string `str` contains a currency symbol "$" that is not valid in a `BigDecimal` object, we remove it using the `replace()` method. The resulting string "6.00" can then be converted to a `BigDecimal` object using its constructor.

Note that you may also need to handle any potential exceptions that can be thrown by the `BigDecimal` constructor, such as `NumberFormatException` if the input string is not a valid decimal number. SkyWard Alpine Login