Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving Whole Number from a Decimal without Rounding? 5

Status
Not open for further replies.

antonio622

Programmer
Jun 10, 2007
20
0
0
US
I have a variable declared as Single and I am trying to retrieve only the whole number without rounding up or down as in the following number:

3.959

I only want to assign 3 in the number above to another variable.

If I do this in code using format number and read only the whole number of course I get the value of 4.

Is there a command or procedure in VB.net to disable rounding off numbers in one instance?
 
You could always do something like
Code:
Dim sTemp as String = "3.959"
Dim sTemp2 as String
sTemp2 = sTemp.Substring(0,(sTemp.IndexOf(".")-1))

Application Developer: VB.Net, Qik III, SQL 2000/2005

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Let's fact it, most computer users have the brain of a Spider Monkey"
~ Bill Gates ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
I do like this approach and if there is no better idea I will use it.

I rather try to keep my number as a numeric variable without converting it to a string as I will use this returned value elsewhere in my code to calculate another numeric variable in my program.

If I do take this approach can I convert the string back into a numeric variable?
 

Code:
Dim float1 as Double = 3.9987
Dim float2 as Double = 3.2341
Dim result as Integer

result = [b]Math.Floor(float1)[/b]
MessageBox.Show(float1 & " becomes " & result)

result = [b]Math.Floor(float2)[/b]
MessageBox.Show(float2 & " becomes " & result)

You can cast variables of type Single to Double to pass into Math.Floor function.

Hope it helps.

 
... or


Code:
    Dim sngl As Single = 3.959
    Dim intgr As Integer = Integer.Parse(sngl.ToString.Split("."c)(0))
    MessageBox.Show(intgr.ToString)


Hope this helps.

[vampire][bat]
 
Thanks to all replies!!!

Once again you have all helped me to move ahead in my project.

I used computerjin approach.
 
talk about using a sledge hammer to crack a nut !

I though I must be going mad until I saw Waynes post.

Fix(Int) and Int(Int) will both return the integer portion of the number

 
AndyLee100

from: Visual Basic Language Reference Int, Fix Functions

Both the Int and Fix functions remove the fractional part of number and return the resulting integer value.

The difference between Int and Fix functions is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.

Fix(number) is equivalent to Sign(number) * Int(Abs(number)).

.. so depending upon the number, there may be rounding using Fix/Int.


Thanks for the star chrissie.

[vampire][bat]
 
I didnt realise that, thanks.

So I guess he should re-write it as:

Fix(Math.Abs(x))
Int(Math.Abs(x))

earthandfire,

What you failed to point out is that Math.Floor(-8.4) will also return 9.
 
AndyLee100,

I agree. But this a "natural" behaviour of negative numbers as defined in Number Theory in Mathematics. The behavior of Math.Floor function follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding toward negative infinity.

AndyLee100, Have a star for your solution.

 
Fix does not round.
8.01, 8.99 return 8
-8.01, -8.99 return -8
If you need unsigned then use the abs function
 
What if you wanted to take the numbers after the decimal place and put that in a variable? Is there a way to do that?
 
Uhm, yes.

The whole number minus then number before the decimal gives you the number after the decimal.

Christiaan Baes
Belgium

"My old site" - Me
 
Code:
    Dim sngl As Single = 3.959
    Dim intgr As Integer = Integer.Parse(sngl.ToString.Split("."c)(1))
    MessageBox.Show(intgr.ToString)


Hope this helps

[vampire][bat]
 
Code:
    Dim sngl As Single = 3.959
    Dim intgr As Integer = CInt((sngl Mod 1) * (10 ^ (Len(sngl.ToString) - 2)))

;P

Only works if 0 < sngl < 10

Regards, Ruffnekk
---
Is it true that cannibals don't eat clowns because they taste funny?
 
Since we're getting crazy with the cheese wizz...
Code:
dim s as single = 3.141
dim i as integer = cint(sngl.tostring.split("."c)(0))

Personally, I'd use cInt or Fix depending on your needs.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top