specify number of digits of Integer using Format VBA - vba

I want to take an integer and display it in digits no matter what the value, e.g Int = 1 to be displayed as 001. No the integer will never be more than 3 digits. Its probably simple and im just missing the obvious.
CStr(Format(Int, 000)
I am concatenating the result as a string. Thanks

This will always show three digits:
MsgBox Format(iMyInt, "000")

I think all you needed was double-quotes:
CStr(Format(Int, "000")

Related

Formatting Main Part of Number While Keeping the decimal part untouched in VBA

I need to format the main part (whole) of a number without touching or affecting the decimal part:
12345.123456 becomes 12,345.123456
123.123 becomes 123.123
12345678.123 becomes 12,345,678.123
123 becomes 123
The fractional part length is variable in length of decimal places and need to be kept untouched (as is).
The formatting applies only to the whole number. Formatting the whole number is simple, but how to not affect the decimal part.
The Format parameter should work with any length of decimal places.
I am using the following:
Format(123456789.12345,"#,#.#############################")
However, the only problem with this solution is:
There is always an assumption on the maximum possible number of decimal places by the number of # used.
If the number is without a fraction say "123.0" or "123", the output will be "123." always with a decimal separator (dot).
Thanks
Like #nicomp said you'll want to break this into two parts.
dim num as string 'or a double converted to a string
dim nums() as string 'array
num = 123456789.123456
nums = split(num, ".") 'break into array at decimal
nums(0) = format(nums(0), "###,###") 'format whole numbers
num = nums(0) & "." & nums(1) 'recombine
This should add a comma after every three whole numbers

Count a Item Integer in a Multi-Line Textbox

I want to play a little with the Textbox. how do I count the items of a textbox? Example: The first line contains the characters: 12 14 16 18 so there are 4 characters but i have one code, and show me 8 character, not 4. how do I display this Count in another textbox? So how do all the characters look into? limited space or comma.
secondTextBox.Text = firstTextBox.Text.Where(Function(x) Not Char.IsWhiteSpace(x)).Count()
this code takes every single digit, I want to take it as a integer. i.e. 12, 14, 16, 18, as a integer.
Try something like this:
Dim number As Integer
secondTextBox.Text = firstTextBox.Text.Split(", ".ToCharArray, StringSplitOptions.RemoveEmptyEntries).Where(Function(x) Integer.TryParse(x, number)).Count
There are lots of ways of doing this, but the simplest which doesn't take account of humans would depend on two things
If all the numbers are on one line and just contain one space between the numbers and no spaces after the last number, just count the number of spaces
To do this in your code you would write
secondTextBox.Text = (firstTextBox.Text.Where(Function(x) Char.IsWhiteSpace(x)).Count() + 1).ToString
If the numbers are on separate lines with no blank lines inbetween the numbers or before or after the numbers, then you would use
secondTextBox.Text = firstTextBox.Lines.Count
When it comes to text searches it may help to take a look at RegEx (Regular Expressions). From your question, it seems you want to count the number of words in a user input. If so, check this question and its first answer.

Incrementing 5 digit number, including leading zeros

I need to generate statement numbers in Access 2016. These numbers need to be 5 digits long, and where the statement number is less than 5 digits, I need to pad the number with leading zeros.
As a starting point, the first statement number will not be padded. So if the last statement number is 96, the next statement number needs to be 00097.
Is it possible (I assume in VBA) to cater for both cases, where the number is 5 digits long, or where it is less.
I was thinking of firstly stripping the leading zeros from the last statement number (if any existed), incrementing the remaining number by one, and then adding leading zeros to make the length of the number 5, but I'm not sure of the most efficient way of doing this.
If it has a leading zero, it will not be a number, but a String. However, as far as it is not quite a bit difference, something like this works:
Sub TestMe()
Dim cnt As Long
Dim myString As String
For cnt = 98 To 120
myString = Format(cnt, "00000")
Debug.Print myString
Next cnt
End Sub
This is what you get in the immediate window:
00098
00099
00100
00101
Yes, just first convert to a number:
NextValue = Format(Val(CurrentValue) + 1, "00000")
You could also use:
NextStateStr = Right("00000" & LastStateNum + 1, 5)

Visual Basic format number to hower many number on left of decimal and 1 decimal without rounding

I am acutally using SSRS but it is for an expression so this is VB code. I am wondering how I would get a number such as 236.4723423 to appear at 236.4 instead of 236.5, so basically I jsut want to truncate it always after 1 decimal.
I tried Format = "N1" this rounds it
I tried Formate = "#######.0" and "######.#" and this rounds it as well.
Any ideas?
value = Math.Floor(value * 10) / 10
Use the format "########.00", then once it's in string form, trim the last char.
Edit:
Dim myString as String
myString = CStr(FORMAT(((SUM(Fields!Shipment_Weight.Value)) / 2000),"######.00"))
myString = myString.Substring(0, myString.Length - 1) & "T"
You will probably want to implement a custom IFormatProvider interface to do this. There is a great example on MSDN here.

Best way to get the first digit from an integer of varying length in VB.NET

I am a newbie to programming and need some help with the basics.
I have a function which takes in an integer value. I want to be able to grab the first digit (or the first and second digits in some cases) of this integer and do something with it.
What is the best way in VB.NET to get the first digit of an integer (or the first and second)?
firstDigit = number.ToString().Substring(0,1)
firstTwoDigits = number.ToString().Substring(0,2);
int.Parse(firstDigit)
int.Parse(firstTwoDigits)
and so forth
I'm not well versed in VB syntax, so forgive me for the syntax errors:
dim i as integer
while i >= 10
i = i \ 10
end while
msgbox "i = " & i
Note, this prints the "first from the left" digit. Like, for "12345" it would print "1".
If you need the digits starting from the end of the integer, just get the modulu result for the tens or the hundreds, according to how many digits you need.
Dim n As Integer
n Mod 10
for the first digit, or:
n Mod 100
for the second and first digits.
If you need the first and second digits from the beginning of the number, there is another answer here which will probably help you.
for first digit you can use:
Dim number As Integer = 234734
Dim first = number.ToString.ToCharArray()(0)
for second digit you can use:
Dim number As Integer = 234734
Dim second = number.ToString.ToCharArray()(1)
This would work. You can use Math.ABS, absolute value, to eliminate negative. The number from left could be replaced by a function if you are using logic, like the overall length of the number, to determine how many of the leading characters you are going to use.
Dim number As Integer = -107
Dim result As String
Dim numberFromLeft As Integer = 2
result = Math.Abs(number).ToString.Substring(0, numberFromLeft)
This results in 10 it is a string but converting it back to a number is easy if you need to. If you need to keep track if it was positive or negative you could use the original value to apply that back to you parsed string.