简体   繁体   中英

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. 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. 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)

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.

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.

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. I'm hoping to shorten the Discount ID and the Min Qty, while keeping the key unique.

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.

Currently, it's very long because it looks like: 2000-995-00216-12 for Discount ID 2000-995, SKU 00216, and Minimum of 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.

 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:

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.

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)))

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.

If the 10s place is odd (10, 34, 51, etc), it puts the letter in the second character.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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