Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33877 > unrolled thread

Re: double-to-int conversion without a cast

Started byArne Vajhøj <arne@vajhoej.dk>
First post2016-02-24 20:28 -0500
Last post2016-02-25 15:21 +0000
Articles 4 — 3 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.


Contents

  Re: double-to-int conversion without a cast Arne Vajhøj <arne@vajhoej.dk> - 2016-02-24 20:28 -0500
    Re: double-to-int conversion without a cast Martin Gregorie <martin@address-in-sig.invalid> - 2016-02-25 12:45 +0000
      Re: double-to-int conversion without a cast Eric Douglas <e.d.programmer@gmail.com> - 2016-02-25 05:36 -0800
        Re: double-to-int conversion without a cast Martin Gregorie <martin@address-in-sig.invalid> - 2016-02-25 15:21 +0000

#33877 — Re: double-to-int conversion without a cast

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-02-24 20:28 -0500
SubjectRe: double-to-int conversion without a cast
Message-ID<nallb7$19k0$1@gioia.aioe.org>
On 2/24/2016 8:07 PM, Stefan Ram wrote:
>    At a point in my course, I have only introduced basic
>    arithmetic operations and calls of static methods and
>    the types »int«, »double«, and »java.lang.String«.
>
>    I already can show them how to convert from int to double
>    (»* 1.0«) or between the numbers and »java.lang.String«
>    (calling conversion methods from the Java SE standard
>    library).
>
>    But I am not aware of how to convert from double to int
>    with only the above means. The standard library is large.
>    Maybe there is a static method that takes a double and returns
>    it's floor or rounded value as an int and I am not aware of it?
>
>    I don't need this. I just ask for curiosity.

Math.round(double) return long. And I do think it makes sense for it
to return long and not int.

I am not aware of any good conversions.

If you are willing to take an ugly one then:

v = Integer.parseInt(String.format("%.0f", x));

Arne


[toc] | [next] | [standalone]


#33879

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2016-02-25 12:45 +0000
Message-ID<namt0l$icf$1@dont-email.me>
In reply to#33877
On Wed, 24 Feb 2016 20:28:05 -0500, Arne Vajhøj wrote:

> 
> Math.round(double) return long. And I do think it makes sense for it to
> return long and not int.
> 
> I am not aware of any good conversions.
> 
> If you are willing to take an ugly one then:
> 
> v = Integer.parseInt(String.format("%.0f", x));
> 
What Arne says.

However it may also be worth telling them about using the Math.floor() 
and Math.ceil() functions in conjunction with Math.round() because there 
are some situations when round() by itself won't do what you want.   

-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#33881

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-02-25 05:36 -0800
Message-ID<20fc7234-8962-4b0d-962b-c1b5c6de17db@googlegroups.com>
In reply to#33879
On Thursday, February 25, 2016 at 7:48:19 AM UTC-5, Martin Gregorie wrote:
> > Math.round(double) return long. And I do think it makes sense for it to
> > return long and not int.
> > 
> > I am not aware of any good conversions.
> > 
> > If you are willing to take an ugly one then:
> > 
> > v = Integer.parseInt(String.format("%.0f", x));
> > 
> What Arne says.
> 
> However it may also be worth telling them about using the Math.floor() 
> and Math.ceil() functions in conjunction with Math.round() because there 
> are some situations when round() by itself won't do what you want.   
> 
> -- 
> martin@   | Martin Gregorie
> gregorie. | Essex, UK
> org       |

Those Math methods are best for simple conversion.  If you plan to do calculations you can pass the double into a BigDecimal, which has methods to return your number as a String as well as the primitive numeric types.

[toc] | [prev] | [next] | [standalone]


#33882

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2016-02-25 15:21 +0000
Message-ID<nan658$mg4$1@dont-email.me>
In reply to#33881
On Thu, 25 Feb 2016 05:36:06 -0800, Eric Douglas wrote:

> On Thursday, February 25, 2016 at 7:48:19 AM UTC-5, Martin Gregorie
> wrote:
>> > Math.round(double) return long. And I do think it makes sense for it
>> > to return long and not int.
>> > 
>> > I am not aware of any good conversions.
>> > 
>> > If you are willing to take an ugly one then:
>> > 
>> > v = Integer.parseInt(String.format("%.0f", x));
>> > 
>> What Arne says.
>> 
>> However it may also be worth telling them about using the Math.floor()
>> and Math.ceil() functions in conjunction with Math.round() because
>> there are some situations when round() by itself won't do what you
>> want.
>> 
>> --
>> martin@   | Martin Gregorie gregorie. | Essex, UK org       |
> 
> Those Math methods are best for simple conversion. 
>
Yep. I was thinking of tasks like converting geographic coordinates 
represented by decimal degrees held in a double into a string like 

ddd mm'ss"X.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web