cancel
Showing results for 
Search instead for 
Did you mean: 

Formatting Number

Former Member
0 Kudos

Hello Experts,

I have a requirement where i have have to format a number like below way..

If Source value is 34300 then target value would be 343.0

if   100 then --   target Val = 1.0

If 50000 then --- target Val = 5.0

Please help me with a UDF

Regards,

Joseph

Accepted Solutions (0)

Answers (2)

Answers (2)

pvishnuvardan_reddy
Active Contributor
0 Kudos

Hi Maverick,

From my understanding your requirement is removing the suffix zeroes in the input and adding suffix .0 to the modified input.

Please try the below udf and check the outcome.

Code snippet:

int i = input.indexOf('0');

String result="";

if(i>0)

result = input.substring(0,i) + ".0";

else

  result = input + ".0";

return result;

vinaymittal
Contributor
0 Kudos

Hi Vishnu,

Just a correction hope you dont mind

in case the number is 234045000

int i = input.indexOf('0'); would give 3 i hope you want 6 for this number as the index of 0


A correct approach might be


int num = Integer.parseInt(field[0]);

int count =0;

while(num%10 ==0)

{

num = num/10;

count++;

}


result.addValue(field[0].substring(0,field[0].length()-count)+".0");





//regex for ending zeroes          can be used alternatively with replaceAll method of string  ([0]+\b)


Harish
Active Contributor
0 Kudos

Hi Joseph,

The logic is not for format number. You are dividing the no and then formatting with decimal. first you need to identify the logic to divide the no (100, 1000 etc.) then use format no to format it with decimal.

regards,

Harish