wracking my brain - converting double value to an integer value - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

wracking my brain - converting double value to an integer value

mdw1982mdw1982 Posts: 124Member
Hi all,

There's something that I've been trying to find out for a few days now. I thought there was a way to do this, but I've been unable to find the information anywhere. And I've been looking at Sun's site as well as doing multiple google searches for the information.

How does one convert a double, say 81.0 to the integer value 81?

thanks,

Mark

Comments

  • : Hi all,
    :
    : There's something that I've been trying to find out for a few days now. I thought there was a way to do this, but I've been unable to find the information anywhere. And I've been looking at Sun's site as well as doing multiple google searches for the information.
    :
    : How does one convert a double, say 81.0 to the integer value 81?
    :
    : thanks,
    :
    : Mark
    :

    There are two easy ways:

    1. You can round the double to a long "long l = Math.round(yourdouble);"
    2. You can truncate the double to a long by simply casting it "long l = (long) yourdouble;"

    In both solutions you can also cast the result to an int "int i = (int) Math.round(yourdouble)" and "int i = (int) yourdouble;"

    tron.
  • rhino_griffrhino_griff Posts: 49Member
    : Hi all,
    :
    : There's something that I've been trying to find out for a few days now. I thought there was a way to do this, but I've been unable to find the information anywhere. And I've been looking at Sun's site as well as doing multiple google searches for the information.
    :
    : How does one convert a double, say 81.0 to the integer value 81?
    :
    : thanks,
    :
    : Mark
    :
    try using the Math.ceil or Math.floor function to round the value!
    Then make it equal to type int..

  • mdw1982mdw1982 Posts: 124Member
    : : Hi all,
    : :
    : : There's something that I've been trying to find out for a few days now. I thought there was a way to do this, but I've been unable to find the information anywhere. And I've been looking at Sun's site as well as doing multiple google searches for the information.
    : :
    : : How does one convert a double, say 81.0 to the integer value 81?
    : :
    : : thanks,
    : :
    : : Mark
    : :
    :
    : There are two easy ways:
    :
    : 1. You can round the double to a long "long l = Math.round(yourdouble);"
    : 2. You can truncate the double to a long by simply casting it "long l = (long) yourdouble;"
    :
    : In both solutions you can also cast the result to an int "int i = (int) Math.round(yourdouble)" and "int i = (int) yourdouble;"
    :
    : tron.
    :


    Thanks Tron,

    The [b]casting[/b] bit was the part I was forgetting. just couldn't remember that for love or money.

    Anyhow, I get what you're saying, but after changing the code accordingly the program is still outputting a double value and not the desired integer value. Below is the method I'm using to generate the random values.

    [code]

    public static int getNum(){
    double theSeed = Math.random();
    int newNum = (int)(Math.round(theSeed * 100) );
    return newNum;
    }


    [/code]
  • mdw1982mdw1982 Posts: 124Member
    : : Hi all,
    : :
    : : There's something that I've been trying to find out for a few days now. I thought there was a way to do this, but I've been unable to find the information anywhere. And I've been looking at Sun's site as well as doing multiple google searches for the information.
    : :
    : : How does one convert a double, say 81.0 to the integer value 81?
    : :
    : : thanks,
    : :
    : : Mark
    : :
    : try using the Math.ceil or Math.floor function to round the value!
    : Then make it equal to type int..
    :

    Thanks rhino,

    however if I code it this way:

    [code]

    public static int getNum(){
    double theSeed = Math.random();
    int newNum = (Math.round(theSeed * 100) );
    return newNum;
    }

    [/code]

    I get a loss of precision error. Not totally unexpected. but if I code the method this way:

    [code]

    public static int getNum(){
    double theSeed = Math.random();
    int newNum = (int) (Math.round(theSeed * 100) );
    return newNum;
    }
    [/code]

    it returns a value, but it's still a double value. for a Java noob...somewhat perplexing. exciting, but perplexing all the same.

    Mark

  • : Anyhow, I get what you're saying, but after changing the code accordingly the program is still outputting a double value and not the desired integer value. Below is the method I'm using to generate the random values.
    :
    : [code]
    :
    : public static int getNum(){
    : double theSeed = Math.random();
    : int newNum = (int)(Math.round(theSeed * 100) );
    : return newNum;
    : }
    :
    :
    : [/code]
    :

    You're sure? If this really returns a double, then there would be something wrong with your JVM - but I can't believe that (if this was an error, then many people would already have reported it). Check your code, maybe it's one of those little nasty bugs ... if you still don't find the reason for this behaviour, then post the code where you are using your "getNum" method.

    Actually I was quite shocked when I'd read your post - so I've just tried your method in a test-class and it's returning ints as expected

    [code]
    public class TestClass {

    public static int getNum(){
    double theSeed = Math.random();
    int newNum = (int) (Math.round(theSeed * 100) );
    return newNum;
    }

    public static void main(String[] args) {
    for (int i=0; i<10; i++)
    System.out.println("" + getNum());
    }

    }
    [/code]

    tron.
  • mdw1982mdw1982 Posts: 124Member
    : You're sure? If this really returns a double, then there would be something wrong with your JVM - but I can't believe that (if this was an error, then many people would already have reported it). Check your code, maybe it's one of those little nasty bugs ... if you still don't find the reason for this behaviour, then post the code where you are using your "getNum" method.
    :
    : Actually I was quite shocked when I'd read your post - so I've just tried your method in a test-class and it's returning ints as expected
    :
    : [code]
    : public class TestClass {
    :
    : public static int getNum(){
    : double theSeed = Math.random();
    : int newNum = (int) (Math.round(theSeed * 100) );
    : return newNum;
    : }
    :
    : public static void main(String[] args) {
    : for (int i=0; i<10; i++)
    : System.out.println("" + getNum());
    : }
    :
    : }
    : [/code]
    :
    : tron.
    :

    Hi Tron,

    You're right. I ran your TestClass and it spat our ints just as you said it would and obviously as the code says it should. I'm still getting doubles from my code, so there's clearly something wrong with my code. I'm going back now to check to see where I've messed things up. If I don't find it I'll post the whole thing. If I find it I'll let you know what it was that was wrong.

    thanks,

    Mark
  • mdw1982mdw1982 Posts: 124Member
    [b][red]This message was edited by mdw1982 at 2003-2-18 13:10:57[/red][/b][hr]
    : You're sure? If this really returns a double, then there would be something wrong with your JVM - but I can't believe that (if this was an error, then many people would already have reported it). Check your code, maybe it's one of those little nasty bugs ... if you still don't find the reason for this behaviour, then post the code where you are using your "getNum" method.
    :
    : Actually I was quite shocked when I'd read your post - so I've just tried your method in a test-class and it's returning ints as expected
    :
    : [code]
    : public class TestClass {
    :
    : public static int getNum(){
    : double theSeed = Math.random();
    : int newNum = (int) (Math.round(theSeed * 100) );
    : return newNum;
    : }
    :
    : public static void main(String[] args) {
    : for (int i=0; i<10; i++)
    : System.out.println("" + getNum());
    : }
    :
    : }
    : [/code]
    :
    : tron.
    :

    Ok...I'm a newbie hammerhead. the problem was that while I was attempting to change the type cast in the class Random I completely forgot about the type that was declared in the class DoubleNode.

    [b]everywhere[/b] you see type "int" it had been type "double" which was causing the output of getNum() to remain a type double instead of recasting the type to type int.

    [code]

    class DoubleNode{

    private int item;
    private IntNode next;

    public DoubleNode( int newItem ){
    item = newItem;
    next = null;
    }

    public DoubleNode( int newItem, DoubleNode nextNode ){
    item = newItem;
    next = nextNode;
    }

    public void setItem( int newItem ){
    item = newItem;
    }

    public int getItem(){
    return item;
    }

    public void setNext(DoubleNode nextNode){
    next = nextNode;
    }

    public DoubleNode getNext(){
    return next;
    }
    }

    [/code]

    Anyway, when I changed the data types in the constructors of the DoubleNode class, Random class outputs integers just as it should be.

    Thanks Tron fer shakin the tree till the [b]correct[/b] fruit fell out! :)

    Mark



  • (snip)

    : Anyway, when I changed the data types in the constructors of the DoubleNode class, Random class outputs integers just as it should be.
    :
    : Thanks Tron fer shakin the tree till the [b]correct[/b] fruit fell out! :)
    :
    : Mark
    :

    Well ... here it comes:

    "How much wood could a woodchucker chuck if a woodchucker would chuck wood?" ;-)

    tron.
  • mdw1982mdw1982 Posts: 124Member
    : (snip)
    :
    : : Anyway, when I changed the data types in the constructors of the DoubleNode class, Random class outputs integers just as it should be.
    : :
    : : Thanks Tron fer shakin the tree till the [b]correct[/b] fruit fell out! :)
    : :
    : : Mark
    : :
    :
    : Well ... here it comes:
    :
    : "How much wood could a woodchucker chuck if a woodchucker would chuck wood?" ;-)
    :
    : tron.
    :

    Well...I'd say howMuchWood() depends on the data type of woodChuck() and how one handles the ChuckWoodNullPointerExcetions! :)

Sign In or Register to comment.