Java Doc for SignificantFigures.java in  » Template-Engine » ostermillerutils » com » Ostermiller » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Template Engine » ostermillerutils » com.Ostermiller.util 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.lang.Number
      com.Ostermiller.util.SignificantFigures

SignificantFigures
public class SignificantFigures extends Number (Code)
A number with an associated number of significant figures. This class handles parsing numbers, determining the number of significant figures, adjusting the number of significant figures (including scientific rounding), and displaying the number. More information about this class is available from ostermiller.org.

When parsing a number to determine the number of significant figures, these rules are used:

  • Non-zero digits are always significant.
  • All zeros between other significant digits are significant.
  • All zeros left of the decimal point between a significant digit and the decimal point are significant.
  • All trailing zeros to the right of the decimal point are significant.
  • If the number is contains no digits other than zero, every zero is significant.

When rounding a number the following rules are used:

  • If the greatest insignificant digit is less than five, round down.
  • If the greatest insignificant digit is greater than five, round up.
  • If the greatest insignificant digit is five and followed by some non-zero digit, round up.
  • If the greatest insignificant digit is five and followed only by zeros, and the least significant digit is odd, round up.
  • If the greatest insignificant digit is five and followed only by zeros, and the least significant digit is even, round down.

Example of using this class to multiply numbers and display the result with the proper number of significant figures:

 String[] arguments = {"1.0", "2.0", ...}
 SignificantFigures number;
 int sigFigs = Integer.MAX_VALUE;
 double result = 1D;
 for (int i=0; i<arguments.length; i++){
     number = new SignificantFigures(arguments[i]);
     sigFigs = Math.min(sigFigs, number.getNumberSignificantFigures());
     result *= number.doubleValue();
 }
 number = new SignificantFigures(result);
 number.setNumberSignificantFigures(sigFigs);
 System.out.println(number);

Example of using this class to add numbers and display the result with the proper number of significant figures:

 String[] arguments = {"1.0", "2.0", ...}
 SignificantFigures number;
 int leastSD = Integer.MIN_VALUE;
 int mostSD = Integer.MIN_VALUE;
 double result = 0D;
 for (int i=0; i<arguments.length; i++){
     number = new SignificantFigures(arguments[i]);
     leastSD = Math.max(leastSD, number.getLSD());
     mostSD = Math.max(mostSD, number.getMSD());
     result += number.doubleValue();
 }
 number = new SignificantFigures(result);
 number.setLMSD(leastSD, mostSD);
 System.out.println(number);

author:
   Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
since:
   ostermillerutils 1.00.00



Constructor Summary
public  SignificantFigures(String number)
     Create a SignificantFigures object from a String representation of a number.
public  SignificantFigures(byte number)
     Create a SignificantFigures object from a byte.
public  SignificantFigures(short number)
     Create a SignificantFigures object from a short.
public  SignificantFigures(int number)
     Create a SignificantFigures object from an integer.
public  SignificantFigures(long number)
     Create a SignificantFigures object from a long.
public  SignificantFigures(float number)
     Create a SignificantFigures object from a float.
public  SignificantFigures(double number)
     Create a SignificantFigures object from a double.
public  SignificantFigures(Number number)
     Create a SignificantFigures object from a java number such as a BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, or Short.

Method Summary
public  bytebyteValue()
     Returns the value of this number as a byte.
public  doubledoubleValue()
     Returns the value of this number as a double.
public  floatfloatValue()
     Returns the value of this number as a float.
public static  Stringformat(byte number, int significantFigures)
     Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display.
public static  Stringformat(double number, int significantFigures)
     Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display.
public static  Stringformat(float number, int significantFigures)
     Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display.
public static  Stringformat(int number, int significantFigures)
     Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display.
public static  Stringformat(long number, int significantFigures)
     Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display.
public static  Stringformat(Number number, int significantFigures)
     Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display.
public static  Stringformat(short number, int significantFigures)
     Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display.
public static  Stringformat(String number, int significantFigures)
     Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display.
public  intgetLSD()
     Get the decimal place of the least significant digit.
public  intgetMSD()
     Get the decimal place of the most significant digit.
public  intgetNumberSignificantFigures()
     Get the number of significant digits.
public  intintValue()
     Returns the value of this number as a int.
public  longlongValue()
     Returns the value of this number as a long.
public  SignificantFiguressetLMSD(int leastPlace, int mostPlace)
     Adjust the number of significant figures such that the least significant digit is at the given place.
public  SignificantFiguressetLSD(int place)
     Adjust the number of significant figures such that the least significant digit is at the given place.
public  SignificantFiguressetNumberSignificantFigures(int significantFigures)
     Adjust the number of digits in the number. Pad the tail with zeros if too short, round the number according to scientific rounding if too long, leave alone if just right.

This method has no effect if this number is not a number or infinity.
Parameters:
  significantFigures - desired number of significant figures.

public  shortshortValue()
     Returns the value of this number as a short.
public  StringtoScientificNotation()
     Formats this number in scientific notation.
public  StringtoString()
     Formats this number.


Constructor Detail
SignificantFigures
public SignificantFigures(String number) throws NumberFormatException(Code)
Create a SignificantFigures object from a String representation of a number.
Parameters:
  number - String representation of the number.
throws:
  NumberFormatException - if the String is not a valid number.
since:
   ostermillerutils 1.00.00



SignificantFigures
public SignificantFigures(byte number)(Code)
Create a SignificantFigures object from a byte.
Parameters:
  number - an 8 bit integer.
since:
   ostermillerutils 1.00.00



SignificantFigures
public SignificantFigures(short number)(Code)
Create a SignificantFigures object from a short.
Parameters:
  number - a 16 bit integer.
since:
   ostermillerutils 1.00.00



SignificantFigures
public SignificantFigures(int number)(Code)
Create a SignificantFigures object from an integer.
Parameters:
  number - a 32 bit integer.
since:
   ostermillerutils 1.00.00



SignificantFigures
public SignificantFigures(long number)(Code)
Create a SignificantFigures object from a long.
Parameters:
  number - a 64 bit integer.
since:
   ostermillerutils 1.00.00



SignificantFigures
public SignificantFigures(float number)(Code)
Create a SignificantFigures object from a float.
Parameters:
  number - a 32 bit floating point.
since:
   ostermillerutils 1.00.00



SignificantFigures
public SignificantFigures(double number)(Code)
Create a SignificantFigures object from a double.
Parameters:
  number - a 64 bit floating point.
since:
   ostermillerutils 1.00.00



SignificantFigures
public SignificantFigures(Number number)(Code)
Create a SignificantFigures object from a java number such as a BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, or Short.
Parameters:
  number - a number.
since:
   ostermillerutils 1.00.00




Method Detail
byteValue
public byte byteValue() throws NumberFormatException(Code)
Returns the value of this number as a byte. the numeric value represented by this object after conversion to type byte.
throws:
  NumberFormatException - if this number cannot be converted to a byte.
since:
   ostermillerutils 1.00.00



doubleValue
public double doubleValue() throws NumberFormatException(Code)
Returns the value of this number as a double. the numeric value represented by this object after conversion to type double.
throws:
  NumberFormatException - if this number cannot be converted to a double.
since:
   ostermillerutils 1.00.00



floatValue
public float floatValue() throws NumberFormatException(Code)
Returns the value of this number as a float. the numeric value represented by this object after conversion to type float.
throws:
  NumberFormatException - if this number cannot be converted to a float.
since:
   ostermillerutils 1.00.00



format
public static String format(byte number, int significantFigures)(Code)
Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display. the number formatted with the correct significant figures
since:
   ostermillerutils 1.02.07



format
public static String format(double number, int significantFigures)(Code)
Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display. the number formatted with the correct significant figures
since:
   ostermillerutils 1.02.07



format
public static String format(float number, int significantFigures)(Code)
Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display. the number formatted with the correct significant figures
since:
   ostermillerutils 1.02.07



format
public static String format(int number, int significantFigures)(Code)
Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display. the number formatted with the correct significant figures
since:
   ostermillerutils 1.02.07



format
public static String format(long number, int significantFigures)(Code)
Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display. the number formatted with the correct significant figures
since:
   ostermillerutils 1.02.07



format
public static String format(Number number, int significantFigures)(Code)
Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display. the number formatted with the correct significant figures
since:
   ostermillerutils 1.02.07



format
public static String format(short number, int significantFigures)(Code)
Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display. the number formatted with the correct significant figures
since:
   ostermillerutils 1.02.07



format
public static String format(String number, int significantFigures) throws NumberFormatException(Code)
Convenience method to display a number with the correct significant digits.
Parameters:
  number - the number to display
Parameters:
  significantFigures - the number of significant figures to display. the number formatted with the correct significant figures
throws:
  NumberFormatException - if the String is not a valid number.
since:
   ostermillerutils 1.02.07



getLSD
public int getLSD()(Code)
Get the decimal place of the least significant digit.

If this number is not a number or infinity Integer.MIN_VALUE will be returned. the decimal place of the least significant digit.
since:
   ostermillerutils 1.00.00




getMSD
public int getMSD()(Code)
Get the decimal place of the most significant digit.

If this number is not a number or infinity Integer.MIN_VALUE will be returned. the decimal place of the least significant digit.
since:
   ostermillerutils 1.00.00




getNumberSignificantFigures
public int getNumberSignificantFigures()(Code)
Get the number of significant digits.

If this number is not a number or infinity zero will be returned. the number of significant digits in this number.
since:
   ostermillerutils 1.00.00




intValue
public int intValue() throws NumberFormatException(Code)
Returns the value of this number as a int. the numeric value represented by this object after conversion to type int.
throws:
  NumberFormatException - if this number cannot be converted to a int.
since:
   ostermillerutils 1.00.00



longValue
public long longValue() throws NumberFormatException(Code)
Returns the value of this number as a long. the numeric value represented by this object after conversion to type long.
throws:
  NumberFormatException - if this number cannot be converted to a long.
since:
   ostermillerutils 1.00.00



setLMSD
public SignificantFigures setLMSD(int leastPlace, int mostPlace)(Code)
Adjust the number of significant figures such that the least significant digit is at the given place. This method may add significant zeros to the end of this number, or remove significant digits from this number.

If all significant digits are removed from this number by truncating to the least significant place, a zero will be created with significant figures from the least to most significant places.

This method has no effect if this number is not a number or infinity.
Parameters:
  leastPlace - the desired place of the least significant digit or Integer.MIN_VALUE to ignore.
Parameters:
  mostPlace - the desired place of the most significant digit or Integer.MIN_VALUE to ignore. this number
since:
   ostermillerutils 1.00.00




setLSD
public SignificantFigures setLSD(int place)(Code)
Adjust the number of significant figures such that the least significant digit is at the given place. This method may add significant zeros to the end of this number, or remove significant digits from this number.

It is possible to remove all significant digits from this number which will cause the string representation of this number to become "NaN". This could become a problem if you are adding numbers and the result is close to zero. All of the significant digits may get removed, even though the result could be zero with some number of significant digits. Its is safes to use the setLMSD() method which will make a zero with the appropriate number of significant figures in such instances.

This method has no effect if this number is not a number or infinity.
Parameters:
  place - the desired place of the least significant digit. this number.
since:
   ostermillerutils 1.00.00




setNumberSignificantFigures
public SignificantFigures setNumberSignificantFigures(int significantFigures)(Code)
Adjust the number of digits in the number. Pad the tail with zeros if too short, round the number according to scientific rounding if too long, leave alone if just right.

This method has no effect if this number is not a number or infinity.
Parameters:
  significantFigures - desired number of significant figures. This number.
since:
   ostermillerutils 1.00.00




shortValue
public short shortValue() throws NumberFormatException(Code)
Returns the value of this number as a short. the numeric value represented by this object after conversion to type short.
throws:
  NumberFormatException - if this number cannot be converted to a short.
since:
   ostermillerutils 1.00.00



toScientificNotation
public String toScientificNotation()(Code)
Formats this number in scientific notation.

A string such as "NaN" or "Infinity" may be returned by this method. representation of this number in scientific notation.
since:
   ostermillerutils 1.00.00




toString
public String toString()(Code)
Formats this number. If the number is less than 10^-3 or greater than or equal to 10^7, or the number might have an ambiguous number of significant figures, scientific notation will be used.

A string such as "NaN" or "Infinity" may be returned by this method. representation of this number.
since:
   ostermillerutils 1.00.00




Methods inherited from java.lang.Number
public byte byteValue()(Code)(Java Doc)
abstract public double doubleValue()(Code)(Java Doc)
abstract public float floatValue()(Code)(Java Doc)
abstract public int intValue()(Code)(Java Doc)
abstract public long longValue()(Code)(Java Doc)
public short shortValue()(Code)(Java Doc)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.