This week's giveaway is in the Cloud/Virtualization forum.We're giving away four copies of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP and have Lucian Maly on-line!See this thread for details.
Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Rob Spoor
• Henry Wong
• Liutauras Vilda
Saloon Keepers:
• Tim Moores
• Carey Brown
• Stephan van Hulst
• Tim Holloway
• Piet Souris
Bartenders:
• Frits Walraven
• Himai Minh
• Jj Roberts

# cast a number from Double to int [SOLVED]

Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
Hi all. I know that normally i can`t cast from a higher type as double to a lower type as int, but i just expect that if i am sure 100% my double number fit in an integer to be able to cast it, and the decimal numbers truncated. But when i try to cast from Double to int didn`t work becouse

Examples:

Chapter 3, Pag 94
I just try to calculate the value of an integer value greater than max short range, put in a short by casting Context Code:

The book "short ticketTake = (short) 1921222  //stored as 20678 "

My code:

calcNr it`s  a Double obj, becouse i need the floor method.
So basically if i try to cast (int)(calcNr) // doesn`t work becouse Double to int can`t cast, but if i just type (int)(calcNr+0) then i think the Double obj will pass the double value and then become castable for int i just suppose it happen this, and the corect version that i found after some times that i tryed the code (int)(calcNr.doubleValue()) in this case the obj will return a double value, castable to int by trouncating the decimal part.
I am not sure why (int)(calcNr+0) works fine.

Saloon Keeper
Posts: 12828
279
• 1
• Number of slices to send:
Optional 'thank-you' note:

Alex Damian wrote:Hi all. I know that normally i can`t cast from a higher type as double to a lower type as int

You can, you just have to perform an explicit cast.

but i just expect that if i am sure 100% my double number fit in an integer to be able to cast it

This has nothing to do with it. You may always explicitly cast one primitive numeric type to another primitive numeric type.

But when i try to cast from Double to int didn`t work

Because Double isn't a primitive, but an object.

I am not sure why (int)(calcNr+0) works fine.

Because the + operator returns int, long, double or String, depending on the types of the operands. calcNr + 0 returns a double, and double can be cast to int. calcNr by itself is a Double, and you may not cast Double to int.

Saloon Keeper
Posts: 23449
159
• 1
• Number of slices to send:
Optional 'thank-you' note:

Stephan van Hulst wrote:Because Double isn't a primitive, but an object.

Boldfaced for emphasis. A Double cannot be cast to a primitive, but the Double intValue() method can do what you want.

For that matter, you can do a Double x; int i = (int)x.doubleValue(); or even (int)x.floatValue(), but that's just making things more complicated.

Alex Damian
Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:

Stephan van Hulst wrote:

Alex Damian wrote:Hi all. I know that normally i can`t cast from a higher type as double to a lower type as int

You can, you just have to perform an explicit cast.

but i just expect that if i am sure 100% my double number fit in an integer to be able to cast it

This has nothing to do with it. You may always explicitly cast one primitive numeric type to another primitive numeric type.

But when i try to cast from Double to int didn`t work

Because Double isn't a primitive, but an object.

I am not sure why (int)(calcNr+0) works fine.

Because the + operator returns int, long, double or String, depending on the types of the operands. calcNr + 0 returns a double, and double can be cast to int. calcNr by itself is a Double, and you may not cast Double to int.

Thank you !!

Alex Damian
Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:

Tim Holloway wrote:

Stephan van Hulst wrote:Because Double isn't a primitive, but an object.

Boldfaced for emphasis. A Double cannot be cast to a primitive, but the Double intValue() method can do what you want.

For that matter, you can do a Double x; int i = (int)x.doubleValue(); or even (int)x.floatValue(), but that's just making things more complicated.

Thank you for the help !!

 I'm sure glad that he's gone. Now I can read this tiny ad in peace! SKIP - a book about connecting industrious people with elderly land owners https://coderanch.com/t/skip-book