You are here
Home > Programming > Java > Karakter Tipe Data BigDecimal di Java

Karakter Tipe Data BigDecimal di Java

Karekter Tipe Data BigDecimal di Java

Tipe data BigDecimal biasa digunakan untuk menampung data nilai uang. Kali ini kita akan coba mempelajari Karakter Tipe Data BigDecimal di Java dalam proses perkalian / multiply.
Sebelum memulai mari kita lihat sedikit informasi mengenai tipe data BigDecimal dari sumber oracle

The BigDecimal class gives its user complete control over rounding behavior, forcing the user to explicitly specify a rounding behavior for operations capable of discarding precision (divide and setScale). Eight rounding modes are provided for this purpose. Two types of operations are provided for manipulating the scale of a BigDecimal: scaling/rounding operations and decimal point motion operations. Scaling/Rounding operations (SetScale) return a BigDecimal whose value is approximately (or exactly) equal to that of the operand, but whose scale is the specified value; that is, they increase or decrease the precision of the number with minimal effect on its value. Decimal point motion operations (movePointLeft and movePointRight) return a BigDecimal created from the operand by moving the decimal point a specified distance in the specified direction; that is, they change a number’s value without affecting its precision.

Tipe data ini memiliki variable index:

  • ROUND_CEILING If the BigDecimal is positive, behave as for ROUND_UP; if negative, behave as for ROUND_DOWN.
  • ROUND_DOWN Never increment the digit prior to a discarded fraction (i.e., truncate).
  • ROUND_FLOOR If the BigDecimal is positive, behave as for ROUND_DOWN; if negative behave as for ROUND_UP.
  • ROUND_HALF_DOWN Behave as for ROUND_UP if the discarded fraction is > .5; otherwise, behave as for ROUND_DOWN.
  • ROUND_HALF_EVEN Behave as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behave as for ROUND_HALF_DOWN if it’s even.
  • ROUND_HALF_UP Behave as for ROUND_UP if the discarded fraction is >= .5; otherwise, behave as for ROUND_DOWN.
  • ROUND_UNNECESSARY This “pseudo-rounding-mode” is actually an assertion that the requested operation has an exact result, hence no rounding is necessary.
  • ROUND_UP Always increment the digit prior to a non-zero discarded fraction.

Memiliki Contructor index:

  • BigDecimal(BigInteger) Translates a BigInteger into a BigDecimal.
  • BigDecimal(BigInteger, int) Translates a BigInteger and a scale into a BigDecimal.
  • BigDecimal(double) Translates a double into a BigDecimal.
  • BigDecimal(String) Constructs a BigDecimal from a string containing an optional minus sign followed by a sequence of zero or more decimal digits, optionally followed by a fraction, which consists of a decimal point followed by zero or more decimal digits.

Dan memiliki Method index:

  • abs() Returns a BigDecimal whose value is the absolute value of this number, and whose scale is this.scale().
  • add(BigDecimal) Returns a BigDecimal whose value is (this + val), and whose scale is MAX(this.scale(), val.scale).
  • compareTo(BigDecimal) Returns -1, 0 or 1 as this number is less than, equal to, or greater than val.
  • divide(BigDecimal, int) Returns a BigDecimal whose value is (this / val), and whose scale is this.scale().
  • divide(BigDecimal, int, int) Returns a BigDecimal whose value is (this / val), and whose scale is as specified.
  • doubleValue() Converts the number to a double.
  • equals(Object) Returns true if x is a BigDecimal whose value is equal to this number.
  • floatValue() Converts this number to a float.
  • hashCode() Computes a hash code for this object.
  • dst… bisa anda lihat langsung di link sumber diatas.

Ok, kita mulai simulasi karakteristik tipe data BigDecimal dalam proses perkalian:

1). Buka editor eclipse anda, lalu create project belajar atau buka project belajar existing Anda

2). Create class “TestBigDecimal” jangan lupa centang “public static void main”

3). Create method static dengan nama perkalianPertama dan bertipe void, seperti terlihat pada cuplikan coding dibawah ini:

4). Tambahkan coding perkalian antara 100 *BigDecimal dan 50 *BigDecimal, berikut full codingnya:

5). Create method static dengan nama perkalianKedua dan bertipe void, untuk perkalian antara 100 *BigDecimal dengan 0.9 *BigDecimal, seperti terlihat pada coding dibawah ini:

6). Coba tebak berapa hasilnya? Anda pasti menjawab 90. tapi kita test nanti ya hasilnya. he…he…

7) Kita lanjut dulu bikin satu method lagi dengan nama perkalianKetiga dan bertipe void, untuk perkalian antara 100 *BigDecimal dengan 90 *BigDecimal dan pembagian 100 *BigDecimal, seperti terlihat pada coding dibawah ini:

8). Coba tebak nilainya sama ga dengan perkalianKedua?

Leave a Reply

Top