How to convert length/distance from one unit to another unit like miles,kilometers,metres,centimeters. etc …


/**
 * @param l length in double which you want to convert
 * @param from Units of length <b>l</b> from which you want to convert
 * @param to Units to which you want to convert your length
 * @return length in double after conversion <br/><br/>
 *
 * @see <code>double d = 0;<br/>
 * d = convertLength(10,Units.KILOMETRE,Units.METRE); <br/>
 * // converts 10 from KILOMETRE to METRE<br/>
 * System.out.println(d); // d=10000<br/><br/>
 * d = convertLength(1,Units.METRE,Units.KILOMETRE); <br/>
 * // converts 1 from METRE to KILOMETRE<br/>
 * System.out.println(d); // d=0.001 <br/></code>
 */
 public static double convertLength(double l,Units from,Units to)
 {
 if( (from==to) || (l<=0) )
 {
 return l;
 }
 else
 {
 double from_length__in_meter = from.length;
 double to_length_in_meter = to.length;

 double metre_len = (1/to_length_in_meter);

 double d = l * from_length__in_meter * metre_len;

 if(d<10e-5||d>10e5)
 {
 DecimalFormat decimalFormat = new DecimalFormat("0.00E00");
 String s = decimalFormat.format(d);
 d = Double.parseDouble(s);
 return d;
 }
 else
 {
 DecimalFormat decimalFormat = new DecimalFormat("0.00");
 String s = decimalFormat.format(d);
 d = Double.parseDouble(s);
 return d;
 }
 }
 }

The Units enum as follows


public enum Units
{
 ANGSTORM(1e-10),
 ASTRONOMICAL_UNIT(1495978714.64e2),
 BARLEYCORN(8.46e-3),
 BOHR(5.3e-11),
 CABLE_LENGTH(185.2),
 CENTIMETRE(0.01),
 CHAIN(20.11684),
 CUBIT(0.5),
 ELL(1.143),
 FATHOM(1.8288),
 FERMI(1e-15),
 FINGER(0.022225),
 FOOT(0.304799735),
 FRENCH(0.0003),
 FURLONG(201.168),
 HAND(0.1016),
 INCH(0.0254),
 KILOMETRE(1000),
 LEAGUE(4828.032),
 LIGHT_YEAR(9.4607304725808e15),
 LIGHT_DAY(2.59020683712e13),
 LIGHT_HOUR(1.0792528488e12),
 LIGHT_MINUTE(1.798754748e10),
 LIGHT_SECOND(299792458),
 LINE(0.002116),
 LINK_GUNTERS_SURVEYORS(0.201168),
 LINK_RAMSDENS_ENGINEERS(0.3048),
 METRE(1),
 MICKEY(1.27e-4),
 MICRON(1e-6),
 MIL(2.54e-5),
 MILE(1609.344),
 MILLIMETRE(1e-3),
 NAIL(0.05715),
 NANOMETER(1e-9),
 NAUTICAL_LEAGUE(5556),
 NAUTICAL_MILE(1853),
 PACE(0.762),
 PALM(0.0762),
 PARSEC(30.857e15),
 POINT(0.000351450),
 QUARTER(0.2286),
 ROD(5.0292),
 ROPE(6.096),
 SPAN(0.2286),
 SPAT(1e12),
 STICK(0.0508),
 PICOMETRE(1e-12),
 X_UNIT(1.002e-13),
 YARD(0.9144);

 Units(double value)
 {
 this.length = value;
 }

 public double length;

}

Advertisements

Tags:

About krishjlk

http://www.facebook.com/krishna.kanth.121

One response to “How to convert length/distance from one unit to another unit like miles,kilometers,metres,centimeters. etc …”

  1. home security says :

    Hello! I understand this is kind of off-topic but I had to ask.
    Does building a well-established blog such as yours require a large amount of work?
    I’m completely new to writing a blog but I do write in my journal daily. I’d like to
    start a blog so I can easily share my experience and thoughts online.
    Please let me know if you have any recommendations or tips for new aspiring bloggers.

    Thankyou!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: