Question:
Data types in Visual Basic?
?
2010-07-09 11:08:08 UTC
Hello guys.

I have a simple question.

if I want to deal with numbers that do not exceed -1000 till +1000 (Not greater or less), and at the same time want to show decimal values as well, which data type should I use to not waste memory?

Currently I used Double, any suggestions?

Next question: How can I decrease the precision? Like I want 4 digits after decimal to be shown.
Example: instead of 141.81941304 I want to show 141.8194

Thanks in advance.
Five answers:
Kenneth
2010-07-09 11:38:36 UTC
Use Single instead of Double. This way you are using 4 bytes instead of 8 bytes.



To get the four digits, try something like this:

Dim result as Single

...

...



LabelqCD.Text = CStr(result.ToString("#,##0.0000"))



Note: This truncates to 4 decimals places. If you need it to round, you may want to look into Math.Round
?
2016-10-18 10:36:49 UTC
in the beginning by no potential ever use a version information sort, its huge gradual and rather undesirable. extremely a version enables all varieties of information, be they integers, doubles or strings. they seem to be a "fail secure" form of variable sort as they are going to oftentimes by no potential with the aid of an errors for being the incorrect sort (as they're all wide-unfold varieties.) besides the fact that if because of this they take in extra area (around 12+ bytes per variable, extra in the event that they carry a string) and are gradual VB will might desire to make certain what form of information they're keeping.
?
2010-07-09 11:13:41 UTC
I think Float would save you some memory but I'm not 100% sure.



as for precision.... I think if you just use the "PRINT USING" and similar statements that should solve the problem.



...but its been awhile since I've played with VB, so I don't know for sure.
BobberKnob
2010-07-09 11:31:15 UTC
the .NET CLR optimizes doubles and int32s



Although it seems wrong, in most cases double will outperform float, even though it is double the size.



It is not useful to think of using a smaller data types to "Save memory" in a .NET environment in many cases.



Stick with int or double for most applications and you'll be fine, in this case Double.



Math.Round(yourNum, 4);
anonymous
2010-07-09 11:11:38 UTC
It's been a long time since I've coded in vB but use float.



Dim var as Float


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...