Search code examples
stringformatpentahomaskkettle

Pentaho convert string to integer with decimal point


I am importing text values into a transformation using a Fixed Width input step. Everything is coming in as a string. I want to convert some of the string values to integers with a decimal point at a specified spot. Here are some examples of the before (left hand side) and expected results (right hand side):

00289 --> 0028.9
01109 --> 0110.9
003201 --> 0032.01

I've tried numerous combinations of the Format mask in a Select Values step (meta data tab) but I can't get the values I'm looking for.

Can you anyone tell me what combination I can try for* Type/Length/Precision/Format/Encoding/Decimal/Group* attributes for these fields to get the desired output?


Solution

  • Have you tried another step the reach your goal? You can try to use e.g. User Defined Java Expression setting it in this way:

    • Java expression: new java.math.BigDecimal(text.substring(0,4) + "." + text.substring(4,text.length()))
    • Value type: BigNumber

    But this will convert your input to:

    00289 --> 28.9
    01109 --> 110.9
    003201 --> 32.01
    

    Because its output is BigNumber format. BigNumber or Number format can be used for decimal numbers. You cannot use Integer for decimals because it has no decimal part.

    If you want a String output leave out the new java.math.BigDecimal() part from the expression above and set Value type to String. It will produce these results:

    00289 --> 0028.9
    01109 --> 0110.9
    003201 --> 0032.01
    

    This is the one suggestion. Of course there are another ways of how to reach your goal.