cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to pad the spaces with the number and align it to the right side.

Former Member
0 Kudos

I have a requirement to generate the output with right alignment of 15 char and 2 decimals.

Example

123456789123456.12

234568.76

12345.98

567.11

12.45

As you can see I am not sure about the input length so I can't used the format number I have tried the format number but the output length exceed the required length.

I have to generate an output of 15 char and 2 decimal

output = (15.2) i.e. total 17 char.

Can somebody tell me how to write the udf to handle this as I am not a java expert and that's why struggling with the code.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

No solution.

Former Member
0 Kudos

Hi,

You can use below UDF to add leading spaces. Decimal conversion, i think you will be bale to do it. Let me know if you face any issue.

 int len=a.length();

int total=b.length();

for(int i=0; i<total-len;i++)
{
a =" "+a;
}
return a; 

Warm Regards,

Gouri

Former Member
0 Kudos

Hi Gouri,

Thanks for your reply.

Could you please tell me how do I fixed the output to (15.2) because with this code I can't restrict the output.

Is it possible you write another code according to the requirement.

I will be very thankful to you.

Example

123456789012345.12

54321.34

678.11

the input field length is unknown and also i need to align the output from right side pad with the spaces on the left.

Former Member
0 Kudos

Hi,

Will give you pointers, I am sure you can write the complete logic with that ...

1. Break the string into two - before and after decimal, using indexOf(). This will return the index as an INT. Keep these two substrings in two variables, say substr1 and substr2 respectively.

2. Now, you have to pad leading zeros in the substr1. So, use - String.format("%05d", substr1).

3. Concat substr1, decimal and substr2.

I haven't tried using String.format on a number with decimal. You may try that and if that works, then the number of steps in your code will be less.

Hope this helps.

Regards,

Neetesh