Java by API/java.math/BigDecimal — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:10, 31 мая 2010
Содержание
- 1 BigDecimal: abs()
- 2 BigDecimal: add(BigDecimal augend)
- 3 BigDecimal: divide(BigDecimal divisor)
- 4 BigDecimal: divide(BigDecimal divisor, int roundingMode)
- 5 BigDecimal: doubleValue()
- 6 BigDecimal: equals(Object x)
- 7 BigDecimal: max(BigDecimal val)
- 8 BigDecimal: min(BigDecimal val)
- 9 BigDecimal: multiply(BigDecimal multiplicand)
- 10 BigDecimal: negate()
- 11 BigDecimal: pow(int n)
- 12 BigDecimal: remainder(BigDecimal divisor)
- 13 BigDecimal.ROUND_DOWN
- 14 BigDecimal.ROUND_HALF_DOWN
- 15 BigDecimal.ROUND_HALF_UP
- 16 BigDecimal.ROUND_UP
- 17 BigDecimal: setScale(int newScale, int roundingMode)
- 18 BigDecimal: subtract(BigDecimal subtrahend)
- 19 BigDecimal: valueOf(long val)
- 20 new BigDecimal(String val)
BigDecimal: abs()
/*
3419229223372036854775809.23343
3419229223372036854775805.23343
1709614611686018427387903.616715
false
3419229223372036854775807.23343
3419229223372036854775807.23343
2.0
1.23343
*/
import java.math.BigDecimal;
public class MainClass {
public static void main(String argv[]) {
BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
BigDecimal second = new BigDecimal("2.0");
System.out.println(first.add(second));
System.out.println(first.subtract(second));
System.out.println(first.divide(second));
System.out.println(first.equals(second));
System.out.println(first.abs());
System.out.println(first.max(second));
System.out.println(first.min(second));
System.out.println(first.remainder(second));
}
}
BigDecimal: add(BigDecimal augend)
/*
3419229223372036854775809.23343
3419229223372036854775805.23343
1709614611686018427387903.616715
false
3419229223372036854775807.23343
3419229223372036854775807.23343
2.0
1.23343
*/
import java.math.BigDecimal;
public class MainClass {
public static void main(String argv[]) {
BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
BigDecimal second = new BigDecimal("2.0");
System.out.println(first.add(second));
System.out.println(first.subtract(second));
System.out.println(first.divide(second));
System.out.println(first.equals(second));
System.out.println(first.abs());
System.out.println(first.max(second));
System.out.println(first.min(second));
System.out.println(first.remainder(second));
}
}
BigDecimal: divide(BigDecimal divisor)
/*
3419229223372036854775809.23343
3419229223372036854775805.23343
1709614611686018427387903.616715
false
3419229223372036854775807.23343
3419229223372036854775807.23343
2.0
1.23343
*/
import java.math.BigDecimal;
public class MainClass {
public static void main(String argv[]) {
BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
BigDecimal second = new BigDecimal("2.0");
System.out.println(first.add(second));
System.out.println(first.subtract(second));
System.out.println(first.divide(second));
System.out.println(first.equals(second));
System.out.println(first.abs());
System.out.println(first.max(second));
System.out.println(first.min(second));
System.out.println(first.remainder(second));
}
}
BigDecimal: divide(BigDecimal divisor, int roundingMode)
import java.math.BigDecimal;
public class Main {
public static void main(String[] argv) throws Exception {
BigDecimal bd1 = new BigDecimal("123456789.0123456890");
// Create via a long
BigDecimal bd2 = BigDecimal.valueOf(123L);
bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
}
}
BigDecimal: doubleValue()
import java.math.BigDecimal;
public class Main {
public static void main(String args[]) {
double r = 3.1234567;
int decimalPlace = 2;
BigDecimal bd = new BigDecimal(r);
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_UP);
r = bd.doubleValue();
System.out.println(r);
}
}
BigDecimal: equals(Object x)
/*
3419229223372036854775809.23343
3419229223372036854775805.23343
1709614611686018427387903.616715
false
3419229223372036854775807.23343
3419229223372036854775807.23343
2.0
1.23343
*/
import java.math.BigDecimal;
public class MainClass {
public static void main(String argv[]) {
BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
BigDecimal second = new BigDecimal("2.0");
System.out.println(first.add(second));
System.out.println(first.subtract(second));
System.out.println(first.divide(second));
System.out.println(first.equals(second));
System.out.println(first.abs());
System.out.println(first.max(second));
System.out.println(first.min(second));
System.out.println(first.remainder(second));
}
}
BigDecimal: max(BigDecimal val)
/*
3419229223372036854775809.23343
3419229223372036854775805.23343
1709614611686018427387903.616715
false
3419229223372036854775807.23343
3419229223372036854775807.23343
2.0
1.23343
*/
import java.math.BigDecimal;
public class MainClass {
public static void main(String argv[]) {
BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
BigDecimal second = new BigDecimal("2.0");
System.out.println(first.add(second));
System.out.println(first.subtract(second));
System.out.println(first.divide(second));
System.out.println(first.equals(second));
System.out.println(first.abs());
System.out.println(first.max(second));
System.out.println(first.min(second));
System.out.println(first.remainder(second));
}
}
BigDecimal: min(BigDecimal val)
/*
3419229223372036854775809.23343
3419229223372036854775805.23343
1709614611686018427387903.616715
false
3419229223372036854775807.23343
3419229223372036854775807.23343
2.0
1.23343
*/
import java.math.BigDecimal;
public class MainClass {
public static void main(String argv[]) {
BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
BigDecimal second = new BigDecimal("2.0");
System.out.println(first.add(second));
System.out.println(first.subtract(second));
System.out.println(first.divide(second));
System.out.println(first.equals(second));
System.out.println(first.abs());
System.out.println(first.max(second));
System.out.println(first.min(second));
System.out.println(first.remainder(second));
}
}
BigDecimal: multiply(BigDecimal multiplicand)
import java.math.BigDecimal;
import javax.swing.JFrame;
public class MainClass extends JFrame {
public static void main(String[] a) {
BigDecimal bn1 = new BigDecimal("98765423462576235623562346234623462.35632456234567890");
BigDecimal bn2 = new BigDecimal("9898234523235624664376437634674373436547.34586558");
BigDecimal bn3 = bn1.multiply(bn2);
System.out.println(bn3);
}
}
BigDecimal: negate()
import java.math.BigDecimal;
public class Main {
public static void main(String[] argv) throws Exception {
BigDecimal bd1 = new BigDecimal("123456789.0123456890");
bd1 = bd1.negate();
}
}
BigDecimal: pow(int n)
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
BigDecimal decimalA = new BigDecimal("123456789012345");
BigDecimal decimalB = new BigDecimal("10");
decimalA = decimalA.add(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.multiply(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.subtract(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.divide(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.pow(2);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.negate();
System.out.println("decimalA = " + decimalA);
}
}
BigDecimal: remainder(BigDecimal divisor)
/*
3419229223372036854775809.23343
3419229223372036854775805.23343
1709614611686018427387903.616715
false
3419229223372036854775807.23343
3419229223372036854775807.23343
2.0
1.23343
*/
import java.math.BigDecimal;
public class MainClass {
public static void main(String argv[]) {
BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
BigDecimal second = new BigDecimal("2.0");
System.out.println(first.add(second));
System.out.println(first.subtract(second));
System.out.println(first.divide(second));
System.out.println(first.equals(second));
System.out.println(first.abs());
System.out.println(first.max(second));
System.out.println(first.min(second));
System.out.println(first.remainder(second));
}
}
BigDecimal.ROUND_DOWN
import java.math.BigDecimal;
public class Main {
public static void main(String[] argv) throws Exception {
int decimalPlaces = 2;
BigDecimal bd = new BigDecimal("123456789.0123456890");
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_DOWN);
String string = bd.toString();
}
}
BigDecimal.ROUND_HALF_DOWN
import java.math.BigDecimal;
public class Main {
public static void main(String args[]) {
BigDecimal rate = new BigDecimal(".03251234");
BigDecimal months = new BigDecimal("12");
BigDecimal monthlyRate = rate
.divide(months, BigDecimal.ROUND_HALF_DOWN);
System.out.println("Annual rate : " + rate);
System.out.println("Monthly rate: " + monthlyRate);
BigDecimal balance = new BigDecimal("10000.0000");
for (int i = 0; i < 12; i++) {
BigDecimal interest = balance.multiply(monthlyRate);
balance = balance.add(interest);
System.out.println("Balance: " + balance);
}
}
}
BigDecimal.ROUND_HALF_UP
import java.math.BigDecimal;
public class Main {
public static void main(String args[]) {
BigDecimal bd = new BigDecimal(3.14159);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(bd);
}
}
//3.14
BigDecimal.ROUND_UP
import java.math.BigDecimal;
public class Main {
public static void main(String[] argv) throws Exception {
BigDecimal bd1 = new BigDecimal("123456789.0123456890");
// Create via a long
BigDecimal bd2 = BigDecimal.valueOf(123L);
bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
}
}
BigDecimal: setScale(int newScale, int roundingMode)
import java.math.BigDecimal;
public class Main {
public static void main(String args[]) {
BigDecimal bd = new BigDecimal(3.14159);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(bd);
}
}
//3.14
BigDecimal: subtract(BigDecimal subtrahend)
/*
3419229223372036854775809.23343
3419229223372036854775805.23343
1709614611686018427387903.616715
false
3419229223372036854775807.23343
3419229223372036854775807.23343
2.0
1.23343
*/
import java.math.BigDecimal;
public class MainClass {
public static void main(String argv[]) {
BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
BigDecimal second = new BigDecimal("2.0");
System.out.println(first.add(second));
System.out.println(first.subtract(second));
System.out.println(first.divide(second));
System.out.println(first.equals(second));
System.out.println(first.abs());
System.out.println(first.max(second));
System.out.println(first.min(second));
System.out.println(first.remainder(second));
}
}
BigDecimal: valueOf(long val)
import java.math.BigDecimal;
public class Main {
public static void main(String[] argv) throws Exception {
// Create via a string
BigDecimal bd1 = new BigDecimal("123456789.0123456890");
// Create via a long
BigDecimal bd2 = BigDecimal.valueOf(123L);
bd1 = bd1.add(bd2);
}
}
new BigDecimal(String val)
import java.math.BigDecimal;
import javax.swing.JFrame;
public class MainClass extends JFrame {
public static void main(String[] a) {
BigDecimal bn1 = new BigDecimal("98765423462576235623562346234623462.35632456234567890");
BigDecimal bn2 = new BigDecimal("9898234523235624664376437634674373436547.34586558");
BigDecimal bn3 = bn1.multiply(bn2);
System.out.println(bn3);
}
}