简体   繁体   English

如何将任何整数值转换为 2 位文本字符串

[英]How do you convert any whole number value into a 2-digit text string

I'm sure there must be a mathematical equation or some type of code that I'm not familiar with which may be capable of generating what I'm looking for, I'm trying to find a way to convert any given whole number up to 300, ideally 9999 if possible, (no decimal places) into a unique 2-digit string.我确定一定有一个数学方程式或某种我不熟悉的代码可能能够生成我正在寻找的东西,我正在尝试找到一种方法来转换任何给定的整数到 300,如果可能的话,最好是 9999,(没有小数位)成一个唯一的 2 位字符串。 I'm okay with that string containing text characters, numbers, or symbols.我可以接受包含文本字符、数字或符号的字符串。

I'm trying to create my own "unique key" for a set of data of our Discounts.我正在尝试为我们的一组折扣数据创建自己的“唯一密钥”。 I'm hoping I can use this to convert our "Min Qty" field from its quantity into some kind of a identifier.我希望我可以使用它来将我们的“Min Qty”字段从它的数量转换为某种标识符。 My original plan, I'm having a hard time figuring out how to make, but would be something like this: a = 2^1 * 9 (2^1=2, so 18) If value is under 18, it would use "A_" where the blank is filled with the multiplier by 2 to reach this value, or 0 if it is 1. B would continue for records where the value is > 18 EG: 1 would equal A0, 17 would equal A8, 42 would equal (B means 18+whatever follows) B3 like: 18+(B=3*2)我最初的计划,我很难弄清楚如何制作,但会是这样的:a = 2^1 * 9 (2^1=2, 所以 18) 如果值低于 18,它将使用“A_”,其中空白用乘数填充 2 以达到该值,如果为 1,则为 0。B 将继续用于值大于 18 的记录 EG:1 将等于 A0,17 将等于 A8,42 将相等(B 表示 18+后面的任何内容) B3 类似:18+(B=3*2)

If anyone has any tips of even something similar, I would be open to it.如果有人有任何类似的提示,我会对此持开放态度。 I would love to find a way to truncate all identifiers I'm putting into this unique key and keep them unique.我很想找到一种方法来截断我放入这个唯一键的所有标识符并保持它们唯一。 Currently I have a 4-digit discount id, 4-digit date "mmyy" start date and end date, and a minimum quantity which is always between 1-400.目前我有一个 4 位数的折扣 ID、4 位数的日期“mmyy”开始日期和结束日期,以及始终在 1-400 之间的最小数量。

In case you're curious of the application, this is just for me to keep record of a unique "discount row" in our report, so when I upload into a local database, I'll have an ID I can match the data on.如果您对该应用程序感到好奇,这只是为了让我在我们的报告中记录一个唯一的“折扣行”,所以当我上传到本地数据库时,我会有一个可以匹配数据的 ID .

EDIT per request I just need a unique key generated by number values.根据请求编辑我只需要一个由数值生成的唯一键。 Currently my unique key is a concatenation of Discount ID,SKU, and Min Qty, but this takes up 16 digits.目前我的唯一键是折扣 ID、SKU 和最小数量的串联,但这需要 16 位数字。 I'm hoping to shorten the Discount ID and the Min Qty, while keeping the key unique.我希望缩短折扣 ID 和最小数量,同时保持密钥唯一。

Discount IDs are normally something like 2000-995, 2001-43, 3031-1, 2050-100, etc. Min Qty doesn't go over 400. The SKU is also a number value, anywhere from 1-6 digits.折扣 ID 通常为 2000-995、2001-43、3031-1、2050-100 等。最小数量不会超过 400 go。SKU 也是一个数字值,介于 1-6 位之间。

Currently, it's very long because it looks like: 2000-995-00216-12 for Discount ID 2000-995, SKU 00216, and Minimum of 12目前,它很长,因为它看起来像:2000-995-00216-12,折扣 ID 2000-995,SKU 00216,最少 12

I'd love to find a way to shorten this in any way possible while keeping unique keys.我很想找到一种方法来以任何可能的方式缩短它,同时保持唯一的键。

The following code will map a given whole number less than 1.296 into a two-digit unique string (consisting of the characters "0,1,2,3,..9" and "A,B,...Z" As you have 36 characters in total you have 36*36=1.296 possible combinations. That will be sufficient for 300 but not for 9.999.下面的代码将 map 一个给定的小于 1.296 的整数转换成一个两位数的唯一字符串(由字符“0,1,2,3,..9”和“A,B,...Z”组成总共有 36 个字符,你有 36*36=1.296 种可能的组合。这对于 300 个字符来说已经足够了,但对于 9.999 个字符来说就足够了。

 Option Explicit

Const ANZ = 36
Function charOne(inpVal As Long) As String
    Dim x As Long
    
    ' first charcater based on the whole fraction
    ' which will be 0 for 36 following numbers
    x = Int(inpVal / ANZ)
    x = CLng(x)  ' just to avoid any floating point issues
    
    charOne = selChar(x)

End Function

Function charTwo(inpVal As Long) As String
    ' second character is based on the modulo
    ' which will count from 0 to 35
    charTwo = selChar(inpVal Mod ANZ)
End Function

Function mapTo2Chars(inpVal As Long) As String
    
    mapTo2Chars = charOne(inpVal) & charTwo(inpVal)
End Function

Function selChar(pos As Long) As String

    Dim ret As String
    Select Case pos
        Case Is <= 9
            ret = CStr(pos)
        Case Is >= 10
            ' A has the Ascii Code 65
            ret = Chr(65 + pos - 10)
    End Select

    selChar = ret

End Function

You can test it with你可以用

Sub testit()
    Debug.Print mapTo2Chars(10)
    Debug.Print mapTo2Chars(887)
End Sub

If any character may be present, use the ascii table:如果可能存在任何字符,请使用 ascii 表:

TwoCharacterText = Chr(Value \ 256) & Chr(Value Mod 256)

But how you intend to use this is not clear for me.但是我不清楚你打算如何使用它。

I ended up figuring it out, Storax gave me an idea which I started testing and it seems to work all the way up to 520.我最终弄明白了,Storax 给了我一个想法,我开始测试它,它似乎一直工作到 520。

Here's the formula I used: =IF(ISEVEN(VALUE(LEFT(TEXT($A1-(FLOOR($A1/100,1)*100),"00"),1))),INDEX(SUBSTITUTE(ADDRESS(1,SEQUENCE(26),4),"1",""),MATCH(TRUE,$A1<SEQUENCE(26)*20,0))&RIGHT($A1,1),RIGHT($A1,1)&INDEX(SUBSTITUTE(ADDRESS(1,SEQUENCE(26),4),"1",""),MATCH(TRUE,$A1<SEQUENCE(26)*20,0)))这是我使用的公式: =IF(ISEVEN(VALUE(LEFT(TEXT($A1-(FLOOR($A1/100,1)*100),"00"),1))),INDEX(SUBSTITUTE(ADDRESS( 1,SEQUENCE(26),4),"1",""),MATCH(TRUE,$A1<SEQUENCE(26)*20,0))&RIGHT($A1,1),RIGHT($A1,1) &INDEX(SUBSTITUTE(ADDRESS(1,SEQUENCE(26),4),"1",""),MATCH(TRUE,$A1<SEQUENCE(26)*20,0)))

It makes it so if the 10s place is even (01, 20, 41, etc), it puts the letter indicating a multiple of 20 in the first character.它使得如果 10 位是偶数(01、20、41 等),它将表示 20 的倍数的字母放在第一个字符中。

If the 10s place is odd (10, 34, 51, etc), it puts the letter in the second character.如果 10 位是奇数(10、34、51 等),则将字母放在第二个字符中。

The other character is just the rightmost digit of the number另一个字符只是数字的最右边的数字

在此处输入图像描述

I may end up trying to add a third character to help shorten 4-digit numbers in the future, but really wanted to keep it to 2-digits so this works for now.我可能最终会尝试添加第三个字符来帮助缩短 4 位数字,但我真的想将其保持为 2 位数字,所以现在可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM