Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Problems with Decimal Symbols
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CooKieLord




PostPosted: Tue Apr 17, 2007 8:14 am   Post subject: Problems with Decimal Symbols

I am working on a program that converts degrees Celsius to degrees Fahrenheit. I am using doubles for variables, and noticed sometimes I get weird numbers like 45.4444444444444444445. I applied a DecimalFormat in order to reduce the decimals to two values. With all that said, instead of showing 45.44, it would show 45,44. I think the reason for this is that I am working in a French environment. As we all know, in French the decimal symbol is a comma as opposed of a period. The reason why I want to change it is that I made the programs using JTextFields. When I enter a string of sorts, it will not work. Here is the code, as I have said before, I work in a French environment, so don't mind the French comments.

code:
//Importer des extensions Java
 import javax.swing.*;
 import java.awt.event.*;
 import java.awt.*;
 import java.text.NumberFormat;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
 import java.util.Locale;


 public class Exercice14_Degrees extends JApplet
    implements ActionListener {

       //Déclaration des éléments qui vont composer le IUG.
       JLabel      etiquetteCelToFar
                  ,etiquetteFarToCel;


       JTextField  champCelToFar
                  ,champFarToCel;

       //Format décimal
       DecimalFormat deuxChiffres = new DecimalFormat ("#.00");
       //DecimalFormatSymbols.setDecimalSeparator (".");
       //NumberFormat nf = NumberFormat.getInstance(Locale.CANADA);

       //DecimalFormat.setDecimalFormatSymbols (".");

       public void init ()
       {

          //Placement des composantes IUG
          Container conteneur = getContentPane ();
          conteneur.setLayout (new FlowLayout ());

          etiquetteCelToFar = new JLabel ("Celsius en Fahrenheit.");
          conteneur.add (etiquetteCelToFar);

          champCelToFar = new JTextField (10);
          conteneur.add (champCelToFar);
          champCelToFar.addActionListener (this);

          etiquetteFarToCel = new JLabel ("Fahrenheit en Celsius.");
          conteneur.add (etiquetteFarToCel);

          champFarToCel = new JTextField (10);
          conteneur.add (champFarToCel);
          champFarToCel.addActionListener (this);

       } //Fin init

       public void actionPerformed (ActionEvent e)
       {
          //Déclaration des variables utilisés pour calculer la puissance.
          double  celsius    = 0.0
                 ,fahrenheit = 0.0;

      //Logique pour la conversion
      //Inclus la conversion de String en Double, d'opération mathématiques et la sortie des résultats.

      // conversion de Celsius en Fahrenheit.
      if (e.getSource () == champCelToFar) {
         celsius     = Double.parseDouble (champCelToFar.getText());
         fahrenheit  = 9.0 / 5.0 * celsius + 32.0;
         champFarToCel.setText (deuxChiffres.format (fahrenheit));
         champCelToFar.setText ("");
         celsius = 0;} //fin if

      //conversion de Fahrenheit en Celsius
      else {
         if (e.getSource() == champFarToCel) {
         fahrenheit  = Double.parseDouble (champFarToCel.getText());
         celsius     = 5.0 / 9.0 * (fahrenheit - 32.0);
         champCelToFar.setText (deuxChiffres.format (celsius));
         champFarToCel.setText ("");
         fahrenheit = 0;} //Fin if
           } //fin else

       }  //fin actionPerformed


 } //fin class


How would I go on about? Is the problem caused by the Locale? If so, how do I change it?
If not, is the key in the DecimalFormatSymbol? I tried something but I'm not sure how to use it.

Can I also get a small evaluation regarding the legibility of my coding?

P.S. On a side note I read something about being able to highlight the syntax. Can someone tell me how?
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Tue Apr 17, 2007 7:15 pm   Post subject: Re: Problems with Decimal Symbols

Ya the code works perfect for me (english machine)

for syntax highlighting its [syntax="java"] code here [/syntax]

(i disabled BBCode so i could show you)
CooKieLord




PostPosted: Sun Apr 29, 2007 3:01 pm   Post subject: Re: Problems with Decimal Symbols

See, I know that it will work perfectly for English machines. The problem is that I am using a French one at school. I have found a solution to my problem.

I used a number format instead of decimal format.

Java:

//Format décimal
DecimalFormat deuxChiffres = new DecimalFormat ("#.00");
//DecimalFormatSymbols.setDecimalSeparator (".");
//NumberFormat nf = NumberFormat.getInstance(Locale.CANADA); 


Would become
Java:
//Format décimal
       //DecimalFormat deuxChiffres = new DecimalFormat ("#.00");
       //DecimalFormatSymbols.setDecimalSeparator (".");
       NumberFormat nf = NumberFormat.getInstance(Locale.CANADA);


At which point, I would remove the commented stuff to avoid confusion and fix the output so that i becomes

Java:

champFarToCel.setText (nf.format (fahrenheit));
champCelToFar.setText (deuxChiffres.format (celsius));


Anyways, thanks for letting me know about the syntax highlighting and sorry it took so long to reply, my school blocked compsci -_-.
Clayton




PostPosted: Sun Apr 29, 2007 5:14 pm   Post subject: RE:Problems with Decimal Symbols

May I ask which school(board) you're from?
CooKieLord




PostPosted: Fri May 04, 2007 6:26 am   Post subject: RE:Problems with Decimal Symbols

I'm from CECLFCE

Conseil des écoles catholique de langue francaise du centre-est, I think.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: