Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,405 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,478 people online right now. Registration is fast and FREE... Join Now!




Rounding Java

 
Reply to this topicStart new topic

Rounding Java

chili5
3 Sep, 2008 - 12:50 PM
Post #1

D.I.C Addict
****

Joined: 28 Dec, 2007
Posts: 700



Thanked: 4 times
My Contributions
I started a course in Java today. One of my exercises is to display 12% of a number rounded to 2 decimal places. I got it all figured out, but something isn't going quite right.

CODE

package ss1;
public class e1b {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        /* Create a program that will populate the total sales
         for a sales representative, and then the program
         will display the 12% commission,
         rounded to 2 decimal places.*/
        
        // TODO code application logic here
        double dblSales = 100.99, dblComm;
        dblComm = dblSales * 0.12;
        System.out.format("12 percent of %f %<.2f is %.2f", dblSales, dblComm);
    }

}



When I run the program I get this output:

QUOTE

12 percent of 100.990000 100.99 is 12.12


Which isn't what I want it to say. I want it to read:
QUOTE
12 percent of 100.99 is 12.12


I don't know where the four zeros on 100.99 are coming from. If I remove %f from my program, the program doesn't work. So I have no idea.
User is offlineProfile CardPM
+Quote Post

capty99
RE: Rounding Java
3 Sep, 2008 - 01:00 PM
Post #2

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,164



Thanked: 16 times
Dream Kudos: 550
My Contributions
you are learning java way different than i did. but this works :

CODE

        double dblSales = 100.99;
        double dblComm = dblSales * 0.12;
        System.out.format("12 percent of " + dblSales + " is %.2f", dblComm);


honestly, i don't understand what is going on, with 2 semesters of java we never did that weird comma variable thing who knows. ANYWAYS. your probably gonna want to take a look at it and make sure it fits your teacher's coding practices, and then i will go back to remembering how little programming i know.

output : 12 percent of 100.99 is 12.12

I would also probably do a System.out.println ... but thats just me.
User is online!Profile CardPM
+Quote Post

chili5
RE: Rounding Java
3 Sep, 2008 - 01:06 PM
Post #3

D.I.C Addict
****

Joined: 28 Dec, 2007
Posts: 700



Thanked: 4 times
My Contributions
I just like to do the comma variable thing, with all variables of the same data type. It makes sense to me. smile.gif

Thanks a lot! Our notes for rounding numbers doesn't make any sense at all.

QUOTE


System.out.format("Here's a formatted float of %d is %f %<+020.10f %n", nNum, dNum);


That's how we learned to format floating point numbers.
User is offlineProfile CardPM
+Quote Post

capty99
RE: Rounding Java
3 Sep, 2008 - 01:09 PM
Post #4

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,164



Thanked: 16 times
Dream Kudos: 550
My Contributions
yeah, i honestly can't follow that. lol. as long as it works.
User is online!Profile CardPM
+Quote Post

chili5
RE: Rounding Java
4 Sep, 2008 - 03:16 AM
Post #5

D.I.C Addict
****

Joined: 28 Dec, 2007
Posts: 700



Thanked: 4 times
My Contributions
Makes no sense to me either. I think using %.2f is a lot easier than that complicated thing above. lol

My first thought was to use Math.round(dblComm, 2); like in JavaScript but that didn't work. lol
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:00PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month