• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

How can I get the first three numbers from a long value

Ok lets say I had a long variable that was something like this

long mylong = 12823450;

Now what I am wanting to know is how can I just get 128 from that long.Thanks for your time and help.
0
DWA1
Asked:
DWA1
  • 2
1 Solution
 
JSandCommented:
what I would do is read this in as an array of characters

ex.  char myNumber[20] = {1,2,8,2,3,4,5,0};

          or
     char myNumber[20];
     scanf("%s",myNumber);

then to get the first three numbers, have another char array:
       char smallNumber[3];

       for(i = 0; i < 3; i++)
       {
         smallNumber[i] = myNumber[i];
       }

then store the array to an integer:
       long myLong;
       myLong = atoi(smallNum);


try that, I hope this helps

--JSand
0
 
JSandCommented:
theres a typo in the scanf statement, there should be a '&' before 'myNumber'.

Sorry
0
 
gj62Commented:
Not really enough info - do you want to extract the leftmost 3 digits?  If so, you could do:

char longStr[11];
long newlong, mylong = 12823450;

sprintf(longStr,"%ld",mylong);
longStr[3] = 0;
newlong = atol(longStr);

If this isn't what you want, please provide more details...
0
 
DWA1Author Commented:
Thanks to you all.Gj62 that was what I was looking for,thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now