Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34449 > unrolled thread
| Started by | Roedy Green <inquiry@mindprod.com> |
|---|---|
| First post | 2016-06-02 22:18 -0700 |
| Last post | 2016-06-05 16:57 -0700 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: double-to-int conversion without a cast Roedy Green <inquiry@mindprod.com> - 2016-06-02 22:18 -0700
Re: double-to-int conversion without a cast Martin Gregorie <martin@address-in-sig.invalid> - 2016-06-03 10:53 +0000
Re: double-to-int conversion without a cast Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-03 08:22 -0400
Re: double-to-int conversion without a cast bursejan@gmail.com - 2016-06-05 16:57 -0700
| From | Roedy Green <inquiry@mindprod.com> |
|---|---|
| Date | 2016-06-02 22:18 -0700 |
| Subject | Re: double-to-int conversion without a cast |
| Message-ID | <im42lbdcl9ar0gsr5kdp2512ri1f86ovv5@4ax.com> |
On 25 Feb 2016 01:07:44 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote: >ouble to int > with only the above means I asked the conversion amanusensis at http://mindprod.com/applet/converter.html it offered: // to int i from double d // best i = (int)d; // or i = (int)Math.round(d); // or i = (int)Math.ceil(d); // or i = (int)Math.floor(d);
[toc] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2016-06-03 10:53 +0000 |
| Message-ID | <nirnit$quc$1@dont-email.me> |
| In reply to | #34449 |
On Thu, 02 Jun 2016 22:18:13 -0700, Roedy Green wrote: > On 25 Feb 2016 01:07:44 GMT, ram@zedat.fu-berlin.de (Stefan Ram) > wrote: > >>ouble to int >> with only the above means > > I asked the conversion amanusensis at > http://mindprod.com/applet/converter.html > > it offered: > > // to int i from double d // best i = (int)d; > // or i = (int)Math.round(d); > // or i = (int)Math.ceil(d); > // or i = (int)Math.floor(d); This works: int n = new Long(Math.round(d)).intValue(); Look Ma, no casts! -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-06-03 08:22 -0400 |
| Message-ID | <nirsr2$ib0$1@dont-email.me> |
| In reply to | #34452 |
On 6/3/2016 6:53 AM, Martin Gregorie wrote:
> On Thu, 02 Jun 2016 22:18:13 -0700, Roedy Green wrote:
>
>> On 25 Feb 2016 01:07:44 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>> wrote:
>>
>>> ouble to int
>>> with only the above means
>>
>> I asked the conversion amanusensis at
>> http://mindprod.com/applet/converter.html
>>
>> it offered:
>>
>> // to int i from double d // best i = (int)d;
>> // or i = (int)Math.round(d);
>> // or i = (int)Math.ceil(d);
>> // or i = (int)Math.floor(d);
>
> This works:
>
> int n = new Long(Math.round(d)).intValue();
>
> Look Ma, no casts!
Too expensive -- just *look* at that wasteful creation of an object!
Tut, tut, tut! Try this:
int n = Integer.parseInt(
Double.toString(d)
.substring(0, Double.toString(d).indexOf('.')))
+ (Double.toString(d).indexOf('.') >= 0
&& Double.toString(d).indexOf('.') + 1
< Double.toString(d).length()
&& Double.toString(d).charAt(
Double.toString(d).indexOf('.') + 1) >= '5'
? Double.toString(d).startsWith("-") ? -1 : 1 : 0);
Look, Ma, no `new'!
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | bursejan@gmail.com |
|---|---|
| Date | 2016-06-05 16:57 -0700 |
| Message-ID | <c3e14960-4c28-4e7c-9921-2d54549b507e@googlegroups.com> |
| In reply to | #34453 |
This code:
public static void main(String[] args) {
double d = Math.PI*100;
int x = (int)d;
System.out.println("x="+x);
}
Prints:
x=314
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Basically you get zero for NaN, min_integer for underflow
and max_integer for overflow.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
See also:
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3
- double to byte, short, char, int, long, or float
If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0.
Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:
If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V.
Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.
Otherwise, one of the following two cases must be true:
The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.
The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web