开发者

Formatting a string from an integer, and keeping leading zeros (PowerBuilder)

So I hav开发者_运维知识库e some code that takes a string and pads some zeros onto the end in order to make it 7 digits in length.

uo_sin.uo_em_sin_number.em_sin_number.text = string(long(ilCurrSin), '#######')

The problem with this is when a number like "001" gets put in, it returns "1000000" when I need it to return "0010000"

I'm guessing there is a variation of the ###### formatting that doesn't lop of leading zeros but I can't find anything.

If it makes any difference, this is in the PowerBuilder 9.0.2 environment.


just use the string function

a required number can be specified with '0' in the format string

uo_sin.uo_em_sin_number.em_sin_number.text = string(long(ilCurrSin), '0000000')


It looks like the data type you are starting from (it's not entirely clear) is a numeric data type. If leading zeroes are important and need to be kept, then a numeric data type is the wrong one to use. When "001" is not the same as "1", and you don't intended to add, subtract, etc..., then what you're dealing with is not a number, but a numeric string. Your problem is one of the ways you can be bitten by this misclassification.

What you can do is change your EditMask's MaskDataType to StringMask!, and use a Mask like "######", which will disallow alpha characters from being data entered.

Now, if you've got the data stored numerically, that's a different issue....

Good luck,

Terry.


If what's below doesn't do what you want you'll have to provide examples of expected inputs and outputs.

outputString = left(inputString + fill("0", 7), 7)

Replacing the constants with variables yields the rpad function available in some languages.

rpad(inputString, len, padString)

return left(inputString + fill(padString, len), len)


There are no leading zero's in an numeric value so you can't directly do what you want.

Assuming that the max value of your numeric is 999 (3 places) as you implied, do something like the following.

  1. Convert the numeric value to a string.

  2. Check the length of the string.

  3. If the string length is less than 3, pad the leading edge with an appropriate number of zero's so the total length is 3.

  4. Pad the trailing string with 4 zero's to get a total of 7.

Cumbersome but given your data type you don't have much choice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜