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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does it has a Mid function for a decimal 3

Status
Not open for further replies.

RSX02

Programmer
May 15, 2003
467
CA
Hi
I would like to retrieve the second number beginning to the left of the point from a decimal .
For example
I have
14 895 - I would like to receive 9
37 258.14 - I would like to receive 5
Does it have any function that does that?
Thanks in advance
 
Untested, but it should be close:

Code:
public function getTensDigit(dValue as double) as integer
 dim sNumber as string
 sNumber=dValue.tostring
 if sNumber.indexof(".") > 1 then 
  return val(sNumber.substring(sNumber.indexof(".")-2,1))
 else
  return 0
 end if
end function

-Rick

----------------------
 
How about using some math?

Dim digit as int
digit = (MyNum / 10) Mod 10

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Why digit diplay me 6?
where chiffre = 12459

Dim digit As Integer
digit = (chiffre / 10) Mod 10
MsgBox(digit)
 
digit = (chiffre / 10) Mod 10 - (((chiffre / 10) Mod 10 )Mod 1)

should work clear off the remainder. you were actually getting 5.600000000003... cast to an INT and it rounded to 6

-Rick

----------------------
 
why when I do this:
Dim temp As Integer
temp = (chiffre / 100) Mod 100 - (((chiffre / 100) Mod 100) Mod 1)
it return me 24?
where chiffre = 12459

I would like to retrieve 124
I'm just not sure how this formula is working...
Thanks again.
 
temp = (chiffre / A) Mod B

A effects the number of digits to the left of the decimal point to end at.

B effects the number of digits to the left of the decimal point to start at.

for example:

(123456 / 1) Mod 10 = 6
(123456 / 1) Mod 100 = 56
(123456 / 1) Mod 1000 = 456
(123456 / 1) Mod 10000 = 3456

(123456 / 10) Mod 10 = 5
(123456 / 10) Mod 100 = 45
(123456 / 10) Mod 1000 = 345
(123456 / 10) Mod 10000 = 2345

hope that makes sence. the 2nd half of the formula is just used to clear the remainder.

-Rick


----------------------
 
RSX02 -
The same technique can be used in other numbering bases. Just change "10" to the base you're using.

So if you're using binary numbers, to see if the 2nd bit is on, you could do this:
Code:
Dim by As Byte
by = (MyNum / 2) Mod 2

Or hexadecimal:
Code:
Dim by As Byte
by = (MyNum / 16) Mod 16

There's a special case for binary numbers -- you use the And operator:
Code:
Dim by As Byte
by = MyNum And 2
This works because the And operates like a mask:
[tab]MyNum = 01000110
[tab]"2" = 00000010
[tab]Result = 00000010
Since the result is non-zero, you know that the bit in position 2 was on.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
also it WILL do this...

myStr=Microsoft.Visualbasic.mid("whatever")

or

myInt=Microsoft.Visualbasic.instr("whatever")

just for backward compliance

John
 
what if I would like to retrieve the first decimal.
For example.
123.456
I would like to retrieve 4
I tried with the minus sign but doesn't seems to work.
Thanks in advance
 
on the number 123.456
this return me 3:
Dim digit As Integer
digit = (chiffre * 1) Mod 10 Mod 10 - (((chiffre / 1) Mod 10) Mod 1)
MsgBox(digit)

this return me 0:
Dim digit As Integer
digit = (chiffre * 10) Mod 10 Mod 10 - (((chiffre / 10) Mod 10) Mod 1)
MsgBox(digit)
 
Dim digit As Integer
digit = (chiffre * 10) Mod 10 - (((chiffre * 10) Mod 10) Mod 1)
MsgBox(digit)

return me 0
 
forget it!
I called my function with the parameter "integer" instead of "decimal".
My mistake!
Sorry for that
Thanks again RIck
 
LOL, I know the fealing, I spent half an hour this morning trying to figure out why I only had write access to the database from my app after changing the config file. Turns out I was changing the config file for another app. heh. Atleast it was a simple mistake :)

-Rick

----------------------
 
heheheh
Happy to see I'm not the only one!

I have another question...After that I think that I'm gonna be a pro with the "Mod".

I'm trying to take off everything after the decimal. So I would like to have an integer. But I can't convert it to the integer because it still have a value after the decimal point.
I tried many things with the mod word. but it seems like I can't get through it.

Dim temp As Decimal
temp = (chiffre * 10) Mod 100000000 - (((chiffre * 10) Mod 100000000) Mod 1)
temp = temp / 10
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top