Jump to content

Help VB 2012 programming

Missioncode

Hey guys I'm in a programming class and I need some help.

 

So here is what I need to do take a number like "1000" and brake it down into the following

1st digit which would be 1

2nd digit which would be 0

multiplier which would be 2

 

so basically  Its a resistor and I need to brake it down into the color code.  I can do the color code but I would like some insight on getting it the individual numbers, so I can do the color code.  

 

Any help would be greatly appreciated.

 

Many Thanks

 

Missioncode

Link to comment
Share on other sites

Link to post
Share on other sites

Like @darkstar said Mod will be your friend.

 

To expand on Mod, the math behind it.  Mod is basically the "remainder" after you divide.  So 100/10 = 10 remainder 0 so 100%10 = 0, 101/10 = 10 remainder 1 so 101%10 = 1

So that means if you have a number like 1000 and want to get the first digit, you just get the mod of 10.

 

So you can exploit the remainders to get each digit.

First digit (right to left) number Mod 10

Second digit (number/10) Mod 10

Third digit (number / 100) Mod 10

...etc

 

So 1050 would look like this

1050 Mod 10 = 0

(1050 / 10) Mod 10 = 105 Mod 10 = 5

(1050 / 100) Mod 10 = 10 Mod 10 = 0

(1050 / 1000) Mod 10 = 1 Mod 10 = 1

Remember that integer division removes the decimal point, which is why 1050 / 100 = 10 and not 10.5

 

 

 

Although, now I think of it when you say 1000 do you means 1000b as in binary (e.g 1000b = 8) and want to get where the 1's and 0's are?

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Although, now I think of it when you say 1000 do you means 1000b as in binary (e.g 1000b = 8) and want to get where the 1's and 0's are?

No, its decimal sorry should have said that. 

Link to comment
Share on other sites

Link to post
Share on other sites

You should've explained to our fellows that the multiplier is actually 10², not 2, although I understand that you have to use 2 to match your colorcode.

It's kinda ghetto solution but it should work. Try to declare the number as a string, so you can use len() and left() functions properly. If you use the len() function on that string it will return the multiplier + 2. For example:

Len("1000") returns 4, so the multiplier is 4 - 2 = 2

Then using Left("1000",1) and Right(Left("1000",2),1) returns the first and second digits respectivly


Sure there are some other ways that envolve more math, but if you don't have any restrition (like the resistance HAVING to be an integer) it'd work just fine

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×