정수 자릿수를 제어하고 싶어졌다. n자리의 정수를 정해진 m자리의 정수로 바꾸고 싶다는 것이다.
예를 들면 123->000123, 2->000002 이런식으로
엑셀에서는 셀 서식으로 간단하게 할 수 있는 것인데 거창하게 말고 완전 간단하게 해결하고 싶었다.
activity를 한개만 사용을 해서!
처음 든 생각은 strText.ToString("000000")
자릿수가 무조건 6자리로 정해져있다면 매우 간편한 방법이 되겠지만 현실은 그렇게 호락호락하지 않았다. 변환해야할 자릿수를 count로 받아오고 있었기 때문이다.
그래서 찾아본 두 번째 방법은 String.Format을 활용하는 것이다.
다음 링크를 참조하였다.
docs.microsoft.com/en-us/dotnet/api/system.string.format?redirectedfrom=MSDN&view=netframework-4.7.2
String.Format Method (System)
Converts the value of objects to strings based on the formats specified and inserts them into another string. If you are new to the String.Format method, see the Get started with the String.Format method section for a quick overview. See the Remarks sectio
docs.microsoft.com
strCount, strText 모두 String변수이다.
String.Format("{0:D"+strCount+"}", CInt(strText))
// 0은 list의 Index이다. String이기때문에 0이 들어간다. 아래 사진은 0대신 1을 넣었을 때 결과이다.
// D는 Standard numeric format strings로 다음 링크에서 자세한 내용을 확인할 수 있다. 정수는 D, 소수는 F, 16진수는 X를 사용한다.
docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
Standard numeric format strings
In this article, learn to use standard numeric format strings to format common numeric types into text representations in .NET.
docs.microsoft.com
// strCount는 늘리고 싶은 자리 수이다. {0:D6}가 되면 6자리로 늘린다는 뜻이다.
// strText의 자리에는 Int type으로 들어가야한다. 그래서 CInt를 해주었다.
끝!
-----------------------
끝이라고는 했지만 Stirng일때도 사용할 수 있는 방법을 알고싶었다!
forum.uipath.com/t/add-dynamic-number-of-leading-zeros-to-string/164341
Add dynamic number of leading zeros to string
Hi, I have a number of IDs that can be various lengths however, I need to add 0s at the start of the ID so that the final length is 14 digits long. For example: 1234 would need to be 00000000001234 456789 would need to be 00000000456789 Is there a simple w
forum.uipath.com
정수여도 사용할 수 있는 방법이 너무나도 간단하게 나와있었다
strText.padleft (CInt(strCount), CChar (“0”))
따로 설명도 필요없을 정도로 너무 간단하고.. 손쉬운 방법이어서 앞으로 이 방법을 자주 사용할 것 같다!
forum 무한적 감사!!
'RPA' 카테고리의 다른 글
[UiPath] 파일 크기 구하기 (2) | 2021.02.08 |
---|---|
[UiPath] DataTable에 이상한 값(-214..)이 나올 때 (0) | 2021.01.12 |
[UiPath] Array에서 최빈값 구하기 (0) | 2020.11.04 |
[UiPath] Selector 대소문자 무시하기 (1) | 2020.10.14 |
[UiPath] UiPath로 암호 압축(zip) 해제하기 (0) | 2020.07.23 |