Format

Format
Fixed
Scientific
General
Latitude
Longitude
Time

Overview

Format provides the ability to control the precision or format of floating point (i.e.: ‘double’) values. This is useful when the numeric formatting provided by writeln is insufficient.

In addition to the control of precision afforded by this class, the string.Pad method may be used on the results of these methods to pad the result to a specific field width. This is useful for producing output with fixed width columns.

See Examples at the bottom for examples on the use of the methods.

Methods

static string Fixed(double value, int precision)
static string Fixed(Vec3 value, int precision)

Return the string representation of ‘value’ using the underlying C++ ‘fixed’ formatting. This is very similar to C ‘%f’ and FORTRAN ‘F’ formatting. ‘precision’ is the number of digits to the right of the decimal place.

static string Scientific(double value, int precision)
static string Scientific(Vec3 value, int precision)

Return a string where ‘value’ is formatted using the underlying C++ ‘scientific’ formatting. This is very similar to the C ‘%e’ or the FORTRAN ‘E’ formatting. ‘precision’ is the number of digits to the right of the decimal place.

static string General(double value, int precision)
static string General(Vec3 value, int precision)

Return a string where ‘value’ is formatted using the underlying C++ ‘defaultfloat’ formatting. The system will choose either fixed or scientific based on the magnitude of ‘value’. ‘precision’ is number of significant digits.

This is the type of formatting provided by writeln for double values, except this provides the ability to specify the precision.

static string Latitude(double value)
static string Latitude(double value, int precision)
static string Latitude(double value, string format, int precision)

Returns a string where ‘value’ is formatted as “dd:mm:ss[.f][n|s]”. The degree field will always contain two digits. The minutes and seconds fields (if selected) will always contain two digits.

The first form uses “dms” format and 2 digits of precision for fractional seconds. The second form also uses “dms” format but allows you to specify the precision for the fractional seconds. The third form allows you to select from additional formats:

  • “dms” - Output in degrees, minutes and seconds. ‘precision’ is number of fractional second digits produced.

  • “dm” - Output in degrees, minutes and fractions of minutes. ‘precision’ is the number of fractional minute digits produced.

  • “d” - Output in degrees and fractions of degrees. ‘precision’ is the number of fractional degree digits produced.

In each case the result will have the trailing ‘n’ or ‘s’ applied to indicate the hemisphere.

static string Longitude(double value)
static string Longitude(double value, int precision)
static string Longitude(double value, string format, int precision)

Returns a string where ‘value’ is formatted as a “[d]dd:mm:ss[.f][e|w]”. The number of degree digits will contain at least two digits, but three will be used if necessary. The minutes and seconds fields (if selected) will always contain two digits.

The first form uses “dms” format and 2 digits of precision for fractional seconds. The second form also uses “dms” format but allows you to specify the precision for the fractional seconds. The third form allows you to select from additional formats:

  • “dms” - Output in degrees, minutes and seconds. ‘precision’ is number of fractional second digits produced.

  • “dm” - Output in degrees, minutes and fractions of minutes. ‘precision’ is the number of fractional minute digits produced.

  • “d” - Output in degrees and fractions of degrees. ‘precision’ is the number of fractional degree digits produced.

In each case the result will have the trailing ‘e’ or ‘w’ applied to indicate the hemisphere.

static string Time(double value)
static string Time(double value, int precision)
static string Time(double value, string format, int precision)

Returns a string where ‘value’ is formatted as “hh:mm:ss[.f]”. The hour field will always contain at least two digits and more if required. The minutes and seconds fields will always contain two digits.

The first form uses “hms” format and 2 digits of precision for fractional seconds. The second form also uses “hms” format but allows you to specify the precision for the fractional seconds. The third form allows you to select from additional formats:

  • “hms” - Output in hours, minutes and seconds. ‘precision’ is the number of fractional second digits produced.

  • “ms” - Output in minutes and seconds.

  • “s” - Output in seconds.

(It would make more sense if “ms” were “hm” and “s” where “h”, but that isn’t what is available in the underlying code that does the formatting.)

Examples

Controlling precision

Code:

Calendar c = WsfDateTime.CurrentTime();
Vec3 sunLocECI = Sun.LocationECI(c);
writeln("Distance to the Sun  : ", Format.Scientific(sunLocECI.Magnitude(), 8));
writeln("Sun ECI Location     : ", Format.Scientific(sunLocECI, 8));
writeln("Earth Semi-Major Axis: ", Format.Fixed(Earth.EQUATORIAL_RADIUS(), 2));
writeln("Earth Semi-Minor Axis: ", Format.Fixed(Earth.POLAR_RADIUS(), 2));
writeln("Earth Mass           : ", Format.Scientific(Earth.MASS(), 8));
writeln("Pi                   : ", Format.Fixed(Math.PI(), 15));
writeln("Feet per meter       : ", Format.Fixed(Math.FT_PER_M(), 8));

Output:

Distance to the Sun  : 1.47272106e+11
Sun ECI Location     : (8.45131060e+10, -1.10660396e+11, -4.79675397e+10)
Earth Semi-Major Axis: 6378137.00
Earth Semi-Minor Axis: 6356752.31
Earth Mass           : 5.97360000e+24
Pi                   : 3.141592653589793
Feet per meter       : 3.28083990

Displaying latitude and longitude

Code:

WsfGeoPoint p = PLATFORM.Location();
writeln("Current position: ", Format.Latitude(p.Latitude()),
        " ", Format.Longitude(p.Longitude()),
        " ", Format.Fixed(p.Altitude(), 1));

Output:

Current position: 40:00:10.71n 90:00:10.31w Alt: 10670.3 m

Displaying time

Code:

Calendar c = Calendar(WsfDateTime.CurrentTime());
double timeOfDay = c.Hour() * 3600.0 + c.Minute() * 60.0 + c.Second();
writeln("The current date and time is: ", c.ToStringDate(), " ", Format.Time(timeOfDay, 5), " UT");

Output:

The current date and time is: jan 25 2018 01:23:48.67890 UT

Tabular output

Code:

writeln("-Altitude- -Pressure- ---Temp--- --Density- -SonicVel-");
writeln("   (m)        (Pa)        (K)      (kg/m3)     (m/s)  ");
writeln("---------- ---------- ---------- ---------- ----------");
Atmosphere a = Atmosphere.Construct("standard");
for (int i = 0; i <= 50000; i = i + 1000)
{
   double alt = i;
   writeln(Format.Fixed(alt, 0).Pad(10), " ",
           Format.Scientific(a.Pressure(alt), 3).Pad(10), " ",
           Format.Fixed(a.Temperature(alt), 2).Pad(10), " ",
           Format.Fixed(a.Density(alt), 4).Pad(10), " ",
           Format.Fixed(a.SonicVelocity(alt), 1).Pad(10));
}

Output:

-Altitude- -Pressure- ---Temp--- --Density- -SonicVel-
   (m)        (Pa)        (K)      (kg/m3)     (m/s)
---------- ---------- ---------- ---------- ----------
         0  1.013e+05     288.15     1.2250      340.3
      1000  8.988e+04     281.65     1.1116      336.4
      2000  7.950e+04     275.15     1.0065      332.5
      3000  7.011e+04     268.65     0.9091      328.6
      4000  6.164e+04     262.15     0.8191      324.6
      5000  5.402e+04     255.65     0.7361      320.5
      6000  4.718e+04     249.15     0.6597      316.4
      7000  4.106e+04     242.65     0.5895      312.3
      8000  3.560e+04     236.15     0.5252      308.1
      9000  3.074e+04     229.65     0.4663      303.8
     10000  2.644e+04     223.15     0.4127      299.5
     ...
     47000  1.109e+02     270.65     0.0014      329.8
     48000  9.776e+01     270.65     0.0013      329.8
     49000  8.617e+01     270.65     0.0011      329.8
     50000  7.595e+01     270.65     0.0010      329.8