Results 1 to 7 of 7
  1. #1
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default Percentage/decimal points?

    Java Code:
    score_percent = score/7.0*100.0;
    score_results.setText("<html>Score: "+score+"/7 <br> That is: "+score_results+"%</html>");
    Erm score_percent is a double type of variable so the result comes out in like xx.xxxxxxxxx% :p Anyway to get rid of some of the decimal places or round?

    And [CODE]score_percent = (int)score/7.0*100.0;

    gives ultra weird errors xD
    Last edited by Exhonour; 01-16-2009 at 09:43 PM.

  2. #2
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    it's because you're casting a double down to an int and then dividing by a number with a decimal place.

    Either remove the decimal place on the 7 and the 100 as you don't need them anyways. or you should just cast the double to a float rather than an int.

  3. #3
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Hmm

    Now it starts to say on the label:
    javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,bor der=,flags=8388608,ma...

    I have no idea what it means lol

    Doesnt matter if im using float, int or double...
    Last edited by Exhonour; 01-16-2009 at 10:53 PM.

  4. #4
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    oh yeah, didn't notice that before, not sure exactly what you're trying to do but I think you need to change the bit that says:

    <br> That is: "+score_results+"%</html>");

    to:

    <br> That is: "+score_percent+"%</html>");

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you can use the number format class or printf
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Omg what a stupid mistake by me, thanks that helps.

  7. #7
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    K so I read some things because wasnt sure why it was giving me error saying I am giving an int or double and it wants float, so I made:

    score_percent = (int)score/7.0f*100.0f;

    That made it with far less decimal points xD

Similar Threads

  1. get percentage formatting to display 2 decimal places
    By gotenks05 in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 05:57 PM
  2. Random Points inside a Polygon
    By nidhirastogi in forum Advanced Java
    Replies: 1
    Last Post: 09-23-2008, 04:28 AM
  3. Dubugger points to incorrect source code
    By gdias in forum Eclipse
    Replies: 0
    Last Post: 09-02-2008, 09:07 PM
  4. Replies: 2
    Last Post: 08-24-2008, 01:30 AM
  5. Replies: 6
    Last Post: 06-12-2008, 10:14 AM

Posting Permissions

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