从整数格式化字符串并保留前导零 (PowerBuilder)
所以我有一些代码,它接受一个字符串并在末尾填充一些零,以使其长度为 7 位数字。
uo_sin.uo_em_sin_number.em_sin_number.text = string(long(ilCurrSin), '#######')
问题是,当输入像“001”这样的数字时,当我需要它返回“0010000”时,它会返回“1000000”,
我猜测 ###### 格式有一种变体,它不会t lop 前导零,但我找不到任何东西。
如果有什么区别的话,那就是在 PowerBuilder 9.0.2 环境中。
So I have 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用字符串函数
即可在格式字符串中用“0”指定所需的数字
just use the string function
a required number can be specified with '0' in the format string
看起来您开始的数据类型(尚不完全清楚)是数字数据类型。如果前导零很重要并且需要保留,那么数字数据类型就不适合使用。当“001”与“1”不同,并且您不打算进行加、减等操作时,那么您处理的不是数字,而是数字字符串。您的问题是您可能受到这种错误分类影响的原因之一。
您可以做的是将 EditMask 的 MaskDataType 更改为 StringMask!,并使用类似“######”的掩码,这将禁止输入数据中的字母字符。
现在,如果您已经以数字方式存储数据,那就是另一个问题了......
祝你好运,
Terry。
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.
如果下面的内容没有达到您想要的效果,您必须提供预期输入和输出的示例。
outputString = left(inputString + fill("0", 7), 7)
用变量替换常量会产生 rpad 功能在某些语言中可用。
rpad(inputString, len, padString)
return left(inputString + fill(padString, len), len)
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)
数值中没有前导零,因此您无法直接执行您想要的操作。
假设您的数字最大值为 999(3 个位置),如您所暗示的,请执行以下操作。
将数值转换为字符串。
检查字符串的长度。
如果字符串长度小于 3,则在前缘填充适当数量的零,使总长度为 3。
用 4 个零填充尾随字符串,总共得到 7。
很麻烦,但考虑到您的数据类型你没有太多选择。
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.
Convert the numeric value to a string.
Check the length of the string.
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.
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.