By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

find a first digit of number with output

P: 1
to find a first digit of number with output in visual basic studio 6.0 plzzz give me program this question
Oct 23 '15 #1
Share this Question
Share on Google+
2 Replies

P: 13
Ok, I don't know if I fully understand your question but I think you just want to find the first digit of a number(from the left) and output it. For example, if you have 123, you want to output 1.

Ok, I will write this in Python because I have not used visual basic at all, so I hope it helps. You basically just repeatedly divide the number with 10 as long as the number is bigger than 10. When you get out of the loop what will have remained is the first digit. In Python it would be something like that:
Expand|Select|Wrap|Line Numbers
  1. n = 123
  2. while(n > 10):
  3.    n = n // 10 # // just means integer division
  4. first_digit = n
I assume that something similar could be done in visual basic.
I want to note an imporant thing though. This method works only for positive integers, so it will not work, say, for something like -123. However, you can check if you have a negative number and if yes, multiply with -1 to work with the opposite number. Now, if you have a floating point number, in Python specifically, you can easily deal with that by doing that: If you find that n is float, then you do: n = int(n)
and you just get the integer part. Because most languages have functions for flooring, cealing etc. I assume that visual basic has also one. I hope that I helped in some way and that I gave you some food for thought.
Oct 24 '15 #2

P: 27
I think your answer looks rather OTT for the question.

Surely the answer would be just to turn the number into a string and take the left-hand character and check the value.
(note: important to trim the string to remove the preceding space associated with numeric values)

Expand|Select|Wrap|Line Numbers
  1. LeftDigit% = val(Left(trim(str(number)), 1))
Nov 24 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.