Class TLcdAngleFormat
- All Implemented Interfaces:
Serializable
,Cloneable
Parses and formats angles. More specifically:
- it parses a string containing angles in any unit (or without any unit) into doubles of the chosen 'program' unit
- it formats a double of the given 'program' unit into a String containing the chosen 'display' unit
Example for display unit 'TLcdAngleUnit.DEGREE
' and program unit 'TLcdAngleUnit.RADIANS
'
- Parsing '360°' will result in 6.283185307179586 (rad)
- Formatting '2' (rad) will result in '114.59°'
By using the ParsePosition, a string containing more than one angle can be parsed too. For instance, the example below parses four angles:
String inputData = " 120\u00B0 3200mil 360 300some text";
TLcdAngleFormat angleFormat = new TLcdAngleFormat();
angleFormat.setProgramUnit(TLcdAngleUnit.DEGREE);
angleFormat.setDisplayUnit(TLcdAngleUnit.DEGREE);
ParsePosition parsePosition = new ParsePosition(0);
Double value1 = (Double) angleFormat.parseObject(inputData, parsePosition); // 120
Double value2 = (Double) angleFormat.parseObject(inputData, parsePosition); // 180
Double value3 = (Double) angleFormat.parseObject(inputData, parsePosition); // 360
Double value4 = (Double) angleFormat.parseObject(inputData, parsePosition); // 300
The resulting values will be 120, 180, 360, and 300.
A subsequent parse will return null
, because "some text" cannot be parsed.
For
it is also possible to specify a custom display format.
E.g. TLcdAngleUnit.DEGREE
DDDMMSS
to have angles formatted as 000°00'00"
After setting the display unit to DEGREE using setDisplayUnit(TLcdAngleUnit.DEGREE)
you can set
setCustomDegreeDisplayPattern(TLcdAngleFormat.DDDMMSS)
.
TLcdAngleFormat angleFormat = new TLcdAngleFormat(TLcdAngleUnit.DEGREE, TLcdAngleUnit.DEGREE);
angleFormat.setCustomDegreeDisplayPattern(TLcdAngleFormat.DDDMMSS);
String valueFormatted = angleFormat.format(123.4687); // 123°28'07"
Note that all measurements are formatted/parsed as is and won't be normalized to an interval such as
[-180, 180] or [0, 360].
E.g. parsing "400°" will return 400 should the program unit be set to TLcdAngleUnit.DEGREE
- Since:
- 2019.1
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.text.Format
Format.Field
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Custom display pattern 000° (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000.0° (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000.00° (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000.000° (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000.0000° (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000.00000° (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000.000000° (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000.0000000° (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000.00000000° (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000°00'00" (can only be used for display unitTLcdAngleUnit.DEGREE
)static final String
Custom display pattern 000°00'00.00" (can only be used for display unitTLcdAngleUnit.DEGREE
) -
Constructor Summary
ConstructorDescriptionCreate an angle format with display unit:TLcdAngleUnit.DEGREE
program unit:TLcdAngleUnit.DEGREE
TLcdAngleFormat
(TLcdAngleUnit aDisplayUnit) Create an angle format with
program unit:TLcdAngleUnit.DEGREE
and the given display unitTLcdAngleFormat
(TLcdAngleUnit aProgramUnit, TLcdAngleUnit aDisplayUnit) Create an angle format with the given program unit and display unit. -
Method Summary
Modifier and TypeMethodDescriptionclone()
format
(Object aObject, StringBuffer aStringBufferToAppendTo, FieldPosition aFieldPosition) Get the custom display pattern for degrees.Returns the display unit.int
Returns the maximum number of fraction digits used to format an angle.int
Returns the minimum number of fraction digits used to format an angle.Returns the program unit.boolean
Returnstrue
if format will add the unit short name,false
otherwise.parseObject
(String aString, ParsePosition aParsePosition) void
setAddUnitShortName
(boolean aAddUnitShortName) Sets if format should add the unit short name.void
setCustomDegreeDisplayPattern
(String aDegreePattern) Set a custom display format for degrees.void
setDisplayUnit
(TLcdAngleUnit aDisplayUnit) Sets the display unit.void
setMaximumFractionDigits
(int aFractionDigitCount) Sets the maximum number of fraction digits that should be used when formatting an angle.void
setMinimumFractionDigits
(int aFractionDigitCount) Sets the minimum number of fraction digits that should be used when formatting an angle.void
setProgramUnit
(TLcdAngleUnit aProgramUnit) Sets the program unit.toString()
Methods inherited from class java.text.Format
format, formatToCharacterIterator, parseObject
-
Field Details
-
DDD
Custom display pattern 000° (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDMMSS
Custom display pattern 000°00'00" (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDMMSSss
Custom display pattern 000°00'00.00" (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDd
Custom display pattern 000.0° (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDdd
Custom display pattern 000.00° (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDddd
Custom display pattern 000.000° (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDdddd
Custom display pattern 000.0000° (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDddddd
Custom display pattern 000.00000° (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDdddddd
Custom display pattern 000.000000° (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDddddddd
Custom display pattern 000.0000000° (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
DDDdddddddd
Custom display pattern 000.00000000° (can only be used for display unitTLcdAngleUnit.DEGREE
)- See Also:
-
-
Constructor Details
-
TLcdAngleFormat
public TLcdAngleFormat()Create an angle format with- display unit:
TLcdAngleUnit.DEGREE
- program unit:
TLcdAngleUnit.DEGREE
- display unit:
-
TLcdAngleFormat
Create an angle format with
- program unit:
TLcdAngleUnit.DEGREE
- and the given display unit
- Parameters:
aDisplayUnit
- the display unit
- program unit:
-
TLcdAngleFormat
Create an angle format with the given program unit and display unit.- Parameters:
aProgramUnit
- the program unitaDisplayUnit
- the display unit
-
-
Method Details
-
getProgramUnit
Returns the program unit.- Returns:
- the program unit.
- See Also:
-
setProgramUnit
Sets the program unit. Format will assume the given measurement is in this unit. Parse will convert the parsed number to this unit.- Parameters:
aProgramUnit
- The program unit.- See Also:
-
getDisplayUnit
Returns the display unit.- Returns:
- the display unit.
- See Also:
-
setDisplayUnit
Sets the display unit.Format.format(Object)
will convert the given measurement to this unit.Format.parseObject(String)
will detect and parse the display unit as well as other units of the same measure type. When no unit name is provided the format assumes the value is in the display unit.- Parameters:
aDisplayUnit
- The display unit.- See Also:
-
isAddUnitShortName
public boolean isAddUnitShortName()Returnstrue
if format will add the unit short name,false
otherwise.- Returns:
true
if format will add the unit short name,false
otherwise.- See Also:
-
setAddUnitShortName
public void setAddUnitShortName(boolean aAddUnitShortName) Sets if format should add the unit short name. Note that parsing supports values with or without unit name, regardless of this setting. This option has no influence when using a custom degree display format along with a display unitTLcdAngleUnit.DEGREE
- Parameters:
aAddUnitShortName
-true
if unit short name should be added,false
otherwise.- See Also:
-
getMinimumFractionDigits
public int getMinimumFractionDigits()Returns the minimum number of fraction digits used to format an angle.- Returns:
- the minimum number of fraction digits used to format an angle.
- See Also:
-
setMinimumFractionDigits
public void setMinimumFractionDigits(int aFractionDigitCount) Sets the minimum number of fraction digits that should be used when formatting an angle. This option has no influence when using a custom degree display format along with a display unitTLcdAngleUnit.DEGREE
- Parameters:
aFractionDigitCount
- The number of fraction digits.- See Also:
-
getMaximumFractionDigits
public int getMaximumFractionDigits()Returns the maximum number of fraction digits used to format an angle.- Returns:
- the maximum number of fraction digits used to format an angle.
- See Also:
-
setMaximumFractionDigits
public void setMaximumFractionDigits(int aFractionDigitCount) Sets the maximum number of fraction digits that should be used when formatting an angle. This option has no influence when using a custom degree display format along with a display unitTLcdAngleUnit.DEGREE
- Parameters:
aFractionDigitCount
- The number of fraction digits.- See Also:
-
getCustomDegreeDisplayPattern
Get the custom display pattern for degrees.- Returns:
- the custom display format for degrees.
- See Also:
-
setCustomDegreeDisplayPattern
Set a custom display format for degrees. The table below details the meaning of each pattern characterSymbol Meaning Example d Integer degrees 354 D Integer degrees with degrees symbol 354° m Integer minutes 54 M Integer minutes with minutes symbol 54' s Integer seconds 54 S Integer seconds with seconds symbol 54" + Coordinate sign. Results in a '-' if the coordinate is negative - Example display patterns are provided e.g.
DDD
orDDDMMSS
In order for the custom display format to be used the display unit should be set to
usingTLcdAngleUnit.DEGREE
setDisplayUnit
.- Parameters:
aDegreePattern
- the pattern to be used when formatting degrees or null when no custom pattern should be used- See Also:
-
format
public StringBuffer format(Object aObject, StringBuffer aStringBufferToAppendTo, FieldPosition aFieldPosition) -
parseObject
- Specified by:
parseObject
in classFormat
-
toString
-
clone
-