Conversion of Java angle string representation and double form

Source: Internet
Author: User

Recently, a GIS Coordinate Transformation task needs to involve this conversion. So we will record it here and present the code. Please criticize and correct it.

 

/**
* Converts an angle to a string representation of degrees, in seconds.
* @ Param Angle
* @ Return
*/
Public static string convert (double angle)
{
Stringbuffer transangle = new stringbuffer ();
Int deg = (INT) angle;
If (deg> 0)
{
Transangle. append (deg + "° ");
}
Double MI = angle-deg;
// Score
Int minute = (INT) (MI * 60 );
Transangle. append (minute + "′");
Double Se = mi * 60-minute;
Transangle. append (Se * 60 + "″");
Return transangle. tostring ();
}
 
/**
* Converts the degree-to-second string representation to a numerical value format.
* @ Param Angle
* @ Return
*/
Public static double converttoangle (string angle)
{
Stringbuffer transangle = new stringbuffer (angle );
// Obtain the string in the unit of second.
String degreestring = transangle. substring (0, transangle. indexof ("° "));
String minutestring = transangle. substring (transangle. indexof ("°") + 1,
Transangle. indexof ("′"));
String secondstring = transangle. substring (transangle. indexof ("'") + 1,
Transangle. indexof ("″"));
// Determine whether the value format is correct
Double Degree = 0;
Double minute = 0;
Double second = 0;
If (checknum (degreestring) & checknum (minutestring) & checknum (secondstring ))
{
Degree = double. parsedouble (degreestring );
Minute = double. parsedouble (minutestring );
Second = double. parsedouble (secondstring );
}
Return degree + (minute * 60 + second)/3600.0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.