Skip to content
Snippets Groups Projects
Commit ba5caf5c authored by Goik Martin's avatar Goik Martin
Browse files

sine rounding using System.out.format()

parent 7351d2a2
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,6 @@
<groupId>de.hdm-stuttgart.de.sd1</groupId>
<artifactId>rounding</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>rounding</name>
......
......@@ -6,49 +6,38 @@ package de.hdm_stuttgart.de.sd1.rounding;
*
*/
public class MathTable {
private final static int numberOfFractionDigits = 3; // Show this number of fractional digits
/**
* @param args unused
*/
public static void main(String[] args) {
int precision = 1;
for (int i = 0; i < numberOfFractionDigits; i++) {
precision *= 10;
}
System.out.println(" x | sin(x)");
System.out.println("----+------");
for (int i = 0; i < 360; i += 5) {
final double
sinValue = Math.sin(i * Math.PI / 180); // Turning degrees into radians
final long
precision = 1000,// Rounding to three decimal places
sinValueRounded = Math.round(precision * sinValue),
digits = sinValueRounded / precision;
long fraction = sinValueRounded % precision;
if (i < 9) { // index number right alignment
System.out.print(" "); // One digit, add two spaces
} else if (i < 99) {
System.out.print(" "); // Two digits, add one space
}
final long
// Rounding to the appropriate number of decimal places
sinValueRounded = Math.round(precision * sinValue);
if (fraction < 0) {
fraction *= -1;
System.out.print(i + " |-" + digits + ".");
} else if (-1 < digits){
System.out.print(i + " | " + digits + ".");
} else {
System.out.print(i + " |" + digits + ".");
}
final double roundedValue = ((double)sinValueRounded) / precision; // double cast: avoid 223 / 1000 == 0
if (fraction < 9) { // Padding fraction with zeros if required
System.out.print("00"); // One digit, add two leading zeros
} else if (fraction < 99) {
System.out.print("0"); // Two digits, add one leading zero
}
final int totalNumberOfDigits = 3 + // Optional '-' + 1 digit + '.' +
numberOfFractionDigits;
System.out.println(fraction);
System.out.format(" %3d|%" + totalNumberOfDigits + "." + numberOfFractionDigits + "f %n", i, roundedValue);
if (0 < i && 0 == i % 20) { // Add extra seperator every four lines
if (0 < i && 0 == i % 20) { // Add extra separator every four lines
System.out.println("----+------");
}
}
......
......@@ -173,7 +173,6 @@
<xsl:variable name="baseDir">
<xsl:text>P/</xsl:text>
<xsl:value-of select="."/>
<xsl:text>/target</xsl:text>
</xsl:variable>
<itemizedlist>
......@@ -194,7 +193,7 @@
</listitem>
<listitem>
<para>Online browsing of <link xlink:href="{$baseDir}/site/apidocs/allclasses-noframe.html">API
<para>Online browsing of <link xlink:href="{$baseDir}/target/site/apidocs/allclasses-noframe.html">API
and implementation</link>.</para>
</listitem>
</itemizedlist>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment