//*********************************************************************************
// 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.
<BR>Called <CODE>Format<B>s</B></CODE> to distinguish from <CODE>java.text.Format</CODE>.
@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 = "°";
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).
<BR>For example, <CODE>186.5</CODE> will be transformed to <CODE>186°30'00"</CODE> ;
<BR><CODE>-6.5</CODE> will be transformed to <CODE>-6°30'00"</CODE>.
<BR><B>Warning</B> : <CODE>x</CODE> must be reduced to its correct interval (ex [0, 360[)
before calling this method.
@param x any number.
@return <CODE>x</CODE> 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; 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 <CODE>nb</CODE> :
<LI>if <CODE>0 <= nb <= 9</CODE>, "0" followed by the digit ;</LI>
<LI>if <CODE>10 <= nb <= 99</CODE>, the two digits ;</LI>
<LI>if <CODE>nb < 0 or nb > 100</CODE>, the String "xx".</LI>
*/
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