//*********************************************************************************
// class tig.Formats
// Software released under the General Public License (version 2 or later), available at
// http://www.gnu.org/copyleft/gpl.html
//*********************************************************************************
package tig;

/******************************************************************************
Class containing constants and static methods for formatting outputs.

Called Formats to distinguish from java.text.Format. @author Thierry Graff @todo Check the whole class for internationalization. @todo throw Exception in addZero? @history dec 29 2000 : Creation. *********************************************************************************/ public class Formats{ //================================================================================= // CONSTANTS //================================================================================= public static final String MINUS = "-"; public static final String DEG = "\u00B0"; public static final String ZERO = "0"; public static final String MINUT = "'"; public static final String SECOND = "\""; //================================================================================= // PUBLIC METHODS //================================================================================= //**************************** doubleToDMS ********************************* /** Expresses a number in format "DMS" (degrees, minuts, seconds).
For example, 186.5 will be transformed to 186 deg 30 prime 00" ;
-6.5 will be transformed to -6 degree 30 prime 00".
Warning : x must be reduced to its correct interval (ex [0, 360[) before calling this method. @param x any number. @return x expressed in "DMS" format. */ public static String doubleToDMS(double x){ int pInt = (x>0) ? (int)Math.floor(x) : (int)Math.ceil(x); // integer part of x double pDec = Math.abs(x - (double)pInt); // decimal part of x int nbMin = (int)Math.floor(pDec*60); // nb of minuts in pDec int nbSec = (int)(Math.floor(pDec*3600)-60*nbMin); //nb of seconds in pDec // add a '0' at the beginning of minuts or seconds if nb < 10. String strMin = (nbMin<10) ? (ZERO + String.valueOf(nbMin)) : (String.valueOf(nbMin)); String strSec = (nbSec<10) ? (ZERO + String.valueOf(nbSec)) : (String.valueOf(nbSec)); String strRes = String.valueOf(pInt)+ DEG + strMin + MINUT + strSec + SECOND; if (x < 0 && x > -1) strRes = MINUS + strRes; return strRes; }// end doubleToDMS //***************************** addZero *************************************** /** Formats a integer to a 2 character long String. Useful for date formatting. @param nb integer number to format. @return A String representation of nb :
  • if 0 <= nb <= 9, "0" followed by the digit ;
  • if 10 <= nb <= 99, the two digits ;
  • if nb < 0 or nb > 100, the String "xx".
  • */ public static String addZero(int nb){ if (nb<0 || nb>99) return "xx"; if (nb<10) return ZERO + String.valueOf(nb); else return String.valueOf(nb); }// end addZero //================================================================================= //================================================================================= // TESTS //================================================================================= //================================================================================= /* // **************** For tests only **************** public static void main(String[] args){ // no complete argument checking if(args[0].equalsIgnoreCase("testDoubleToDMS")) testDoubleToDMS(); else{ String possibleArgs = "'testDoubleToDMS'"; System.out.println("first argument must be " + possibleArgs); } }// end main // **************** For tests only **************** private static void testDoubleToDMS(){ double d1 = 10.654; double d2 = -10.654; System.out.println("d1 : " + doubleToDMS(d1)); System.out.println("d2 : " + doubleToDMS(d2)); }//end testDoubleToDMS */ }//end class Formats

    Ads help maintain this website.