• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

long to Integer conversion

 
Nicky narayan
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to convert an long value to Intger.
Like:
long =1282900123450l;
System.out.println("x value is -->"+x);
Integer y=(int)x;
System.out.println("y value is -->"+y);

The result is :
x value is -->1282900123450
y value is -->-1295098054


can anyone help me on this how to convert long to Integer value without getting -ve value.It is very urgent to me.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your above code you are getting different answers because your long is too big to fit in an int, so when you cast your long to an int it is chopping off bits to make it fit. May I ask why you have to convert a long to an Integer?
 
Nicky narayan
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am getting Date variable as a long value
After that i have to convert it to Integer for my requirement.

Please suggest me how to convert it.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicky narayan wrote:
I am getting Date variable as a long value
After that i have to convert it to Integer for my requirement.

Please suggest me how to convert it.


If the value is bigger than an int range, what is the purpose of using int/Integer there?
 
Nicky narayan
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have to convert it like that only

is there any chance to avoid -ve value while converting long to int
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicky narayan wrote:i have to convert it like that only

is there any chance to avoid -ve value while converting long to int



Think about what you are asking. If I asked you to convert the number 42 to a single digit number, can you do it? And if you can't, how do you expect the computer to do it?

In this case, you are asking to convert a big number to a type that can't hold that number, how do you expect the computer to do it?

Henry
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my understanding, if your long is small enough to fit into an int I THINK (emphasis on that, because I'm not sure) that you can cast it to an int and it might retain the same value. I'm not sure if it drops the bits from the left or right (I believe it is left). However, if you have a value too big for an int and cast it to an int, I'm pretty sure there is no way to keep the value. At that point, you are asking an int to hold something too big for it, which isn't possible.

Edit: I still recommend against the cast. It's the same as trying to put all the water currently in a 10 gallon bucket into a 5 gallon bucket. Sure the 10 gallon may only be holding 4 gallons, but there is no way to be sure it isn't holding over 5 gallons.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A long is 64 bits. An int is 32 bits. Not all values that fit in a long will fit in an int, because the int is smaller. If the number is too large to fit in an int or Integer, there is no way to make it fit.

Dates are often stores as 64-bit numbers. You can't fit those in an int.

So, there is no solution to your problem. You need to talk to whoever gave you the requirements and explain that it's not possible.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicky narayan wrote: . . . It is very urgent to me.
We don't use the word "urgent" on this website.

Also, to be pedantic, you should distinguish int from Integer and from integer. They all mean different things to Java programmers.
 
Daniel Gen Li
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol. it's true that I don't see why you'd desperately want to convert the long into an int when it's going to be truncated. but for future reference. If you want to see if a value in a long will fit into an int, you can check by conparing your long to Integer.MAX_VALUE or Integer.MIN_VALUE.
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W. Joe Smith wrote:From my understanding, if your long is small enough to fit into an int I THINK (emphasis on that, because I'm not sure) that you can cast it to an int and it might retain the same value. I'm not sure if it drops the bits from the left or right (I believe it is left). However, if you have a value too big for an int and cast it to an int, I'm pretty sure there is no way to keep the value. At that point, you are asking an int to hold something too big for it, which isn't possible.

You're completely right on all accounts. Truncating is always done from the left.
 
Mario Busch
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if its - however - needed to get it into a int, fragment the long-parts of the date you need for your int (depending on your needs which we dont know) and put only those parts into the int you really need. maybe you only need ddmmyyyy f.e.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Busch: welcome to the Ranch
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably do the fragmenting of a long into two ints with the >> operator and 0x20 (= 32) and two casts.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic