Hi everybody, I need some help with my first Java program. My teacher don't teach really good ... I have a problem to solve in my script. At that time all are okay but all number must be rounding to 2 decimal :S

That's all my Java program (He is in french but if you need some explain ask me)

/**
 * @(#)billetski.java
 *
 *
 * @author 
 * @version 1.00 2009/10/5
 */
import javax.swing.JOptionPane;	// requise pour les fenêtres de lectures-écritures
public class billetski {
        
    /**
     * Creates a new instance of <code>billetski</code>.
     */
    public billetski() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    	 
        //constantes
        double  PUBLICITE  = 0.18;
        double ENTRETIEN  = 0.26;
        double PROFIT   = 0.14;
        double TAXELOISIR = 3;
        double TPS        = 0.05;
        double TVQ        = 0.075;
       
        //variables
        double coutBase;
        double montantTotal;
        double montantTPS;
        double montantTVQ;
        double montantPublicite;
        double montantEntretien;
        double montantProfit;
        double pourcCoutBase;
        double pourcTPS;
        double pourcTVQ;
        double pourcPub;
        double pourcEntretien;
        double pourcTaxeLoisir;
        double pourcProfit;
        
        	
        //But du programme
         JOptionPane.showMessageDialog(
         	null, "Ce programme permet de calculer le coût de chaque parti du coût total d'un billet de ski.","But du programme",JOptionPane.INFORMATION_MESSAGE);
        coutBase = Integer.parseInt(
        	         JOptionPane.showInputDialog("Entrer le coût de base du billet."));
        	         
        montantPublicite = (coutBase*PUBLICITE);

        montantEntretien = (coutBase*ENTRETIEN);

        montantProfit =(coutBase*PROFIT);

        montantTPS = (coutBase*TPS);

        montantTVQ = (montantTPS*TVQ);

        montantTotal = ((coutBase+ montantPublicite + montantProfit + montantEntretien) + montantTPS + montantTVQ + TAXELOISIR);

        pourcCoutBase = ((coutBase/montantTotal)*100);

        pourcEntretien = ((montantEntretien/montantTotal)*100);

        pourcProfit = ((montantProfit/montantTotal)*100);

        pourcPub = ((montantPublicite/montantTotal)*100);

        pourcTaxeLoisir = ((TAXELOISIR/montantTotal)*100);
        
        pourcTPS = ((montantTPS/montantTotal)*100);
        
        pourcTVQ = ((montantTVQ/montantTotal)*100);
        
        JOptionPane.showMessageDialog(
        	null, "Cout de base:    " + coutBase + "           " + pourcCoutBase + " %" +
        	    "\nPublicité:            " + montantPublicite + "             " + pourcPub + " %" +
        	    "\nEntretien:            " + montantEntretien + "             " + pourcEntretien + " %" +
        	    "\nProfit:                   " + montantProfit + "            " + pourcProfit + " %" + 
        	    "\nTPS:                      " + montantTPS + "               " + pourcTPS + " %" +
        	    "\nTVQ:                      " + montantTVQ + "           " + pourcTVQ + " %" +
        	    "\nTaxe Loisir          " + TAXELOISIR + "               " + pourcTaxeLoisir + " %" +
        	    "\n" +
        	    "\nMontant Total:    " + montantTotal, "Coût du billet",JOptionPane.INFORMATION_MESSAGE );        
        
    }
}

Edited 7 Years Ago by guilh_22: n/a

I'm not sure I know exactly what you are asking.
If all you need is to round to 2 decimal places, here is an example to help you:

double d = 1.234567;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.print(df.format(d));

Yes is what I try but nothing work that say I can't because I use double ...

Can you show me an example in my own program ?

I try it

montantPublicite = (coutBase*PUBLICITE);
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.print(df.format(montantPublicite));

But it show this in my general output I want to stock it in the variable montantPublicite

Edited 7 Years Ago by guilh_22: n/a

System.out.print( df.format(montantPublicite) );

"System.out.print" takes what is inside the parenthesis and displays it. If you want whatever is inside the parenthesis displayed somewhere else then take it and put wherever you want.

This article has been dead for over six months. Start a new discussion instead.