Results 1 to 5 of 5
  1. #1
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default Java does not read the decimals?

    Hello. I was going to post this on the Appleta' site, as it's a problem from an applet, but as I overloaded it with threads today and I think it's a general Java problem, I decided to post it here.

    Well, the thing is that I've got a varyable named "x", and I set it at 1. Summarizing it all, t when I substract 0.05 from that 1, the applet takes it as 0.

    What can I do to solve it and make Java take it as 0.95? Thanks!

    PS: if it's a specific problem of applets, tell me and I'll change the thread to the applets' site.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    What you are describing is integer arithmetic. An int variable can only hold whole numbers. If you want floating point numbers define the variable as double.
    It has nothing to do with where the code is executed.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Remember that you have to declare (==say whether it's an int or a double etc) a variable before you can assign (==set, add etc) a values to it.

    Java Code:
    // int foo = 1;
    double foo = 1.0;
    foo -= 0.05;
    System.out.println(foo);

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    <soapbox>
    Learning about primitives should be just about the first thing you learn. How the heck do you get to writing applets without knowing that you need a double or float to hold a floating point number?
    </soapbox>

  5. #5
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    <soapbox>
    Learning about primitives should be just about the first thing you learn. How the heck do you get to writing applets without knowing that you need a double or float to hold a floating point number?
    </soapbox>
    Of course I know that primitive... And of course I've learnt about primitive before programming applets. It's just that I've been too long using "int", "int", "int"... and that I've almost never used other kind of numbers in other of languages... So it's easy to forgot some primitive things like that one, at least at my experience level...

Similar Threads

  1. Replies: 8
    Last Post: 06-06-2011, 02:27 AM
  2. BIG decimals
    By xael in forum New To Java
    Replies: 17
    Last Post: 10-04-2010, 10:14 AM
  3. Decimals in java help!!
    By Gold in forum New To Java
    Replies: 3
    Last Post: 12-09-2009, 11:25 PM
  4. Replies: 5
    Last Post: 10-17-2008, 02:13 PM
  5. Replies: 1
    Last Post: 07-02-2008, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •