从整数格式化字符串并保留前导零 (PowerBuilder)

发布于 2024-10-30 15:40:59 字数 312 浏览 1 评论 0原文

所以我有一些代码,它接受一个字符串并在末尾填充一些零,以使其长度为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞取 2024-11-06 15:40:59

只需使用字符串函数

即可在格式字符串中用“0”指定所需的数字

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

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')
七月上 2024-11-06 15:40:59

看起来您开始的数据类型(尚不完全清楚)是数字数据类型。如果前导零很重要并且需要保留,那么数字数据类型就不适合使用。当“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.

情未る 2024-11-06 15:40:59

如果下面的内容没有达到您想要的效果,您必须提供预期输入和输出的示例。

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)

欲拥i 2024-11-06 15:40:59

数值中没有前导零,因此您无法直接执行您想要的操作。

假设您的数字最大值为 999(3 个位置),如您所暗示的,请执行以下操作。

  1. 将数值转换为字符串。

  2. 检查字符串的长度。

  3. 如果字符串长度小于 3,则在前缘填充适当数量的零,使总长度为 3。

  4. 用 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.

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文