mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add test
This commit is contained in:
parent
424e6e817e
commit
fca32ecefd
@ -76,4 +76,28 @@ public class MathUtil {
|
||||
public static List<String[]> combinationSelect(String[] datas, int m) {
|
||||
return new Combination(datas).select(m);
|
||||
}
|
||||
|
||||
/**
|
||||
* 金额元转换为分
|
||||
*
|
||||
* @param yuan 金额,单位元
|
||||
* @return 金额,单位分
|
||||
* @since 5.7.11
|
||||
*/
|
||||
public static long yuanToCent(double yuan) {
|
||||
return new Money(yuan).getCent();
|
||||
}
|
||||
|
||||
/**
|
||||
* 金额分转换为元
|
||||
*
|
||||
* @param cent 金额,单位分
|
||||
* @return 金额,单位元
|
||||
* @since 5.7.11
|
||||
*/
|
||||
public static double centToYuan(long cent) {
|
||||
long yuan = cent / 100;
|
||||
int centPart = (int) (cent % 100);
|
||||
return new Money(yuan, centPart).getAmount().doubleValue();
|
||||
}
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ public class Money implements Serializable, Comparable<Money> {
|
||||
* <p>
|
||||
* 创建一个具有金额<code>yuan</code>元<code>cent</code>分和缺省币种的货币对象。
|
||||
*
|
||||
* @param yuan 金额元数。
|
||||
* @param yuan 金额元数,0的情况下表示元的部分从分中截取
|
||||
* @param cent 金额分数。
|
||||
*/
|
||||
public Money(long yuan, int cent) {
|
||||
@ -110,15 +110,19 @@ public class Money implements Serializable, Comparable<Money> {
|
||||
* <p>
|
||||
* 创建一个具有金额<code>yuan</code>元<code>cent</code>分和指定币种的货币对象。
|
||||
*
|
||||
* @param yuan 金额元数。
|
||||
* @param yuan 金额元数,0的情况下表示元的部分从分中截取
|
||||
* @param cent 金额分数。
|
||||
* @param currency 货币单位
|
||||
*/
|
||||
public Money(long yuan, int cent, Currency currency) {
|
||||
this.currency = currency;
|
||||
|
||||
if(0 == yuan) {
|
||||
this.cent = cent;
|
||||
} else{
|
||||
this.cent = (yuan * getCentFactor()) + (cent % getCentFactor());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器。
|
||||
|
@ -86,6 +86,12 @@ public class LocalDateTimeUtilTest {
|
||||
Assert.assertEquals("2020-01-23", localDate.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseSingleMonthAndDayTest() {
|
||||
LocalDate localDate = LocalDateTimeUtil.parseDate("2020-1-1", "yyyy-M-d");
|
||||
Assert.assertEquals("2020-01-01", localDate.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
|
23
hutool-core/src/test/java/cn/hutool/core/math/MoneyTest.java
Normal file
23
hutool-core/src/test/java/cn/hutool/core/math/MoneyTest.java
Normal file
@ -0,0 +1,23 @@
|
||||
package cn.hutool.core.math;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MoneyTest {
|
||||
|
||||
@Test
|
||||
public void yuanToCentTest() {
|
||||
final Money money = new Money("1234.56");
|
||||
Assert.assertEquals(123456, money.getCent());
|
||||
|
||||
Assert.assertEquals(123456, MathUtil.yuanToCent(1234.56));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void centToYuanTest() {
|
||||
final Money money = new Money(1234, 56);
|
||||
Assert.assertEquals(1234.56D, money.getAmount().doubleValue(), 2);
|
||||
|
||||
Assert.assertEquals(1234.56D, MathUtil.centToYuan(123456), 2);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user