I have a string entered by user 25|35|40. I have format any number below 100=25+35+40 to this format. For instance,
5 --> 05|00|00
30 --> 25|05|00
65 --> 25|35|05
The order of 25|35|40 should be maintained. This is similar to date formatting MM:dd:yyyy, in that we know month cannot exceed 12 and date cannot exceed 31. but here any value can occur. the value in its position determine the maximum value. Another example,
If the user string is 40|110|2500|350
5 --> 05|000|0000|000
100 --> 40|060|0000|000
450 --> 40|110|0300|000
2900 --> 40|110|2500|250
If the number exceeds the total 3000=40+110+2500+350, I can make it as number -= 3000.
Currently I am trying to format it using custom code which will check for the number and will create the required output string. Is there any inbuilt format API available in java?
There is no public API for such a specific use case. This is one way to solve it:
prints
Or, to generalise it: