Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

메모장

BigDecimal 본문

Java

BigDecimal

doopang 2023. 1. 5. 09:25
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@DisplayName("BigDecimal Test")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class BigDecimalTest {

  BigDecimal bigDecimal1;
  BigDecimal bigDecimal2;

  @Test
  @Order(0)
  void add() {
    BigDecimal result = bigDecimal1.add(bigDecimal2);
    Assertions.assertThat(result).isEqualByComparingTo(BigDecimal.valueOf(0.3));
  }

  @Test
  @Order(1)
  void subtract() {
    BigDecimal result = bigDecimal2.subtract(bigDecimal1);
    Assertions.assertThat(result).isEqualByComparingTo(BigDecimal.valueOf(0.1));
  }

  @Test
  @Order(2)
  void multiple() {
    BigDecimal result = bigDecimal1.multiply(bigDecimal2);
    Assertions.assertThat(result).isEqualByComparingTo(BigDecimal.valueOf(0.02));
  }

  @Test
  @Order(3)
  void divide() {
    BigDecimal result = bigDecimal2.divide(bigDecimal1, 0, RoundingMode.HALF_UP);
    Assertions.assertThat(result).isEqualByComparingTo(BigDecimal.valueOf(2));
  }

  @Test
  @Order(4)
  void compareTo() {
    boolean result = bigDecimal1.compareTo(bigDecimal2) <= 0; // bigDecimal1 <= bigDecimal2
    Assertions.assertThat(result).isTrue();
  }

  @BeforeEach()
  void define() {
    bigDecimal1 = new BigDecimal("0.1");
    bigDecimal2 = BigDecimal.valueOf(0.2);
  }
}

 

FrontEnd
Terminal

npm i decimal.js

 

Main.vue

import Decimal from 'decimal.js'

'Java' 카테고리의 다른 글

CustomUserDetails, CustomUserDetailsService  (0) 2023.01.10
Optional  (0) 2023.01.09
테스트 코드 기본 세팅  (0) 2023.01.04
JPA Pageable 이용한 페이징 처리  (0) 2022.12.27
Rest Api 응답  (0) 2022.12.26