Public Group

# Stoping VB.Net Rounding Numbers

This topic is 4001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, quick and simple. How do i get VB.NET to stop rounding my numbers? Here is the code i have the problem with:
 P1Level = 1 + (P1Experience / 10)


Whats happening is i want the player to go up to level 2 after it reaches 10 exp, level 3 after 20 exp, level 4 after 30 exp ... you get the idea. but basically what VB.NET is doing is rounding my numbers up, e.g when the player is at 5 - 9 exp it rounds it up to the next level ... and so on for the other levels. Can anyone tell me how to make it stop rounding up so that at exp 9 it will still be level 1? Thanks

##### Share on other sites
If you need precise division, you should cast to an appropriate data type first. By default, an expression will take on the type of its leftmost token. I'm guessing that PlExperience is an int, in which case the division by ten will result in an unwanted rounding. To evaluate the whole thing as a floating value, you should cast to a floating-point type. I'm not very familiar with VB.NET, but I believe it would go something like this:

1.0 + (CSng(PlExperience) / 10)

And to round this down, rather than to the nearest integer, call the floor function:

PlLevel = Math.Floor(1.0 + (CSng(PlExperience) / 10))

Edit: EnochDagor speaks the truth.

[Edited by - TheAdmiral on November 3, 2007 9:18:09 AM]

##### Share on other sites
In VB.NET, when you divide an integer by another integer, the compiler returns the result as a Double. However, if you do not use Option Strict (which I highly advise turning this on), you rely on VB.NET's rounding system when it does the implicit conversion back to an Int.

You can explicitly tell the compiler to use floating point math by adding an f to the end of the 10.

P1Level = 1 + (P1Experience / 10.0f) 'the division is done as a single

Now, an easy fix to this whole thing is actually a performance enhancement at the same time...

P1Level = 1 + (P1Experience \ 10) 'the division is done as an integer

notice the direction of the divider line. That indicates to the compiler to do an integer-based division and to ignore any remainder. This type of division is 10 times faster (sometimes even more) then the normal division.

The one caveat with this division is that it ALWAYS rounds down. Subsequently, if you are working with non-integer data types, you could use:

P1Level = 1 + Math.Floor(P1Experience / 10.0f) 'division is done as a single, but the remainder is truncated.

Hope that helps.

-E

##### Share on other sites
Thanks alot! This was exactly what i was looking for. I really apreciate this!

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632940
• Total Posts
3009328
• ### Who's Online (See full list)

There are no registered users currently online

×