r/visualbasic • • Aug 15 '19

Limiting number of characters in string, Noob question

Hello All,

I've written a quick program to convert a C Temp Value to F Type Value. It works perfect. However, I just want it show the first two numbers. For instance, I want it to show 74 rather than 74.533.

Here is what I wrote:

Dim tempF As String

tempF = ((tempC * (9 / 5)) + 32)

label4.text = TempF

How do I truncate the value?

Thank you very much!

EDIT: Quick typo fix

6 Upvotes

6 comments sorted by

5

u/[deleted] Aug 15 '19

You can just make tempF a integer, should give the result your looking for,

3

u/MattDLD Aug 15 '19

That worked. Thank you!

2

u/non-stick-rob Aug 15 '19 edited Aug 15 '19

label4.text = TempF(string.remove(1))

basically that line means : remove anything after the 2nd character in the string

remember it's zero indexed, ie.. counting starts at zero 0 and you only want to keep chars in position 0 and 1

2

u/MattDLD Aug 16 '19

Thank you. That’s the answer I was looking for.

4

u/snang Moderator Aug 15 '19

You can also use Math.Floor to achieve this.

4

u/bz922x Aug 15 '19

Since what you really want to do is format the data for output, use the Format command to convert a numeric value to a string https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.strings.format?view=netframework-4.8