MDi JLib
1.7.4

com.microdeveloper.common
Class Num

java.lang.Object
  extended by com.microdeveloper.common.Num

public class Num
extends Object

Provides methods for number manipulation.

Copyright©2007 MicroDeveloper, Inc.

Since:
1.7.4
Version:
1.0
Author:
MicroDeveloper, Inc.

Constructor Summary
Num()
           
 
Method Summary
static int byteToPositiveInt(byte b)
          Convert a possibly negative valued byte to a positive value integer.
static double decimalDegrees(String text)
          Pass in a string that represents some form of lat or long and a decimal value is returned.
static int endian(int x)
          Reverse the byte order of a four byte integer.
static short endian(short x)
          Swap the upper and lower bytes of a two byte integer.
static boolean inRange(char x, char min, char max)
          A convenience method to test if a character is within a particular range.
static boolean inRange(double x, double min, double max)
          A convenience method to test if a double is within a particular range.
static boolean inRange(int x, int min, int max)
          A convenience method to test if an integer is within a particular range.
static byte positiveIntToByte(int i)
          Convert an integer possibly negative valued byte to a positive value integer.
static double round(double value, int decimalPlace)
          Simple rounding function - why doesn't Java do this?
static int[] toIntArray(Set values)
          Each value will be cast to Integer and the values will be stored in an array.
static Set toSet(int[] values)
          Each int in the array is encapsulated in an Integer object and added to the set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Num

public Num()
Method Detail

endian

public static short endian(short x)
Swap the upper and lower bytes of a two byte integer.

This is useful when you need to convert between Intel formatted integers(little endian) and Java/Motorola formatted integers(big endian).

Parameters:
x - The number to be converted.

Returns:
The converted number.


endian

public static int endian(int x)
Reverse the byte order of a four byte integer.

This is useful when you need to convert between Intel formatted integers(little endian) and Java/Motorola formatted integers(big endian).

If the original hex number was ABCD, the new hex number will be DCBA.

Parameters:
x - The number to be converted.

Returns:
The converted number.


byteToPositiveInt

public static int byteToPositiveInt(byte b)
Convert a possibly negative valued byte to a positive value integer.

A Java byte has a value of -128 to 127. With eight bits and no sign, the negative numbers could be represented as a value of 128 to 255. This method makes such a conversion, but stores the result in an integer since Java does not support unsigned bytes.

Parameters:
b - The byte with a possibly negative value.

Returns:
An integer with a value of 0 to 255.


positiveIntToByte

public static byte positiveIntToByte(int i)
Convert an integer possibly negative valued byte to a positive value integer.

A Java byte has a value of -128 to 127. With eight bits and no sign, the negative numbers could be represented as a value of 128 to 255. This method makes the conversion from 0-255 to -128-127.

This method assumes your integer will be of the range of 0 to 255. Using a value outside of this range could generate an exception.

Parameters:
i - The integer in the range of 0 to 255.

Returns:
A byte with a possibly negative value.


inRange

public static boolean inRange(int x,
                              int min,
                              int max)
A convenience method to test if an integer is within a particular range.

Parameters:
x - The value to test.

min - The minimum value for x.

max - The maximum value for x.

Returns:
true if ( x >= min ) and ( x <= max ).


inRange

public static boolean inRange(double x,
                              double min,
                              double max)
A convenience method to test if a double is within a particular range.

Parameters:
x - The value to test.

min - The minimum value for x.

max - The maximum value for x.

Returns:
true if ( x >= min ) and ( x <= max ).


inRange

public static boolean inRange(char x,
                              char min,
                              char max)
A convenience method to test if a character is within a particular range.

Parameters:
x - The character to test.

min - The minimum value for x.

max - The maximum value for x.

Returns:
true if ( x >= min ) and ( x <= max ).


decimalDegrees

public static double decimalDegrees(String text)
Pass in a string that represents some form of lat or long and a decimal value is returned.


toSet

public static Set toSet(int[] values)
Each int in the array is encapsulated in an Integer object and added to the set.


toIntArray

public static int[] toIntArray(Set values)
Each value will be cast to Integer and the values will be stored in an array.

This will throw a casting exception if the objects are not all Integer!


round

public static double round(double value,
                           int decimalPlace)
Simple rounding function - why doesn't Java do this?


MDi JLib
1.7.4

Copyright©2001-2007 MicroDeveloper, Inc. All Rights Reserved.