Forum Discussion

shiva1020243's avatar
shiva1020243
Occasional Contributor
3 years ago

Format data from a source in string to our own format

Hi team,

 

can somebody pls help me?

i have an interest rate coming from a source is "00550" and "02000"

 

How can I convert it to 0.550 or 0.550?

02.000 or 2.000?

 

Thanks

3 Replies

  • richie's avatar
    richie
    Community Hero
    Hey shiva1020243,

    Can you provide more info to clarify?

    When you say "source", are you using the datasource functionality? If "yes", how are you using it?
    Or, is the numerics a value in a response?

    Cheers,

    Rich
  • TNeuschwanger's avatar
    TNeuschwanger
    Champion Level 2

    Hello shiva1020243 

     

    Here is some groovy script code that can do what you request.  Given your ambiguous requirement, I made best guess at a response...   🙂  

    There are two sections of the code that are identical just to show different originating source values and their outcome.

     

    Regards,

    Todd

     

    import java.text.DecimalFormat;
    
    df = new DecimalFormat("#0.000");
    
    
    interestRateStr = "00550";
    interestRateInt = interestRateStr.toInteger();
    interestRateFloat = interestRateInt / 1000;
    log.info "interestRateFloat=$interestRateFloat";
    finalValue = df.format(interestRateFloat);
    log.info "finalValue=$finalValue";
    
    interestRateStr = "02000";
    interestRateInt = interestRateStr.toInteger();
    interestRateFloat = interestRateInt / 1000;
    log.info "interestRateFloat=$interestRateFloat";
    finalValue = df.format(interestRateFloat);
    log.info "finalValue=$finalValue";

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thanks everyone!

      Hi shiva1020243, does the advice from the Community help? Please let us know.