Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34453
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: double-to-int conversion without a cast |
| Date | 2016-06-03 08:22 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <nirsr2$ib0$1@dont-email.me> (permalink) |
| References | <conversion-20160225020052@ram.dialup.fu-berlin.de> <im42lbdcl9ar0gsr5kdp2512ri1f86ovv5@4ax.com> <nirnit$quc$1@dont-email.me> |
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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web