This commit is contained in:
Looly 2021-08-30 12:52:10 +08:00
parent 424e6e817e
commit fca32ecefd
4 changed files with 60 additions and 3 deletions

View File

@ -76,4 +76,28 @@ public class MathUtil {
public static List<String[]> combinationSelect(String[] datas, int m) { public static List<String[]> combinationSelect(String[] datas, int m) {
return new Combination(datas).select(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();
}
} }

View File

@ -97,7 +97,7 @@ public class Money implements Serializable, Comparable<Money> {
* <p> * <p>
* 创建一个具有金额<code>yuan</code><code>cent</code>分和缺省币种的货币对象 * 创建一个具有金额<code>yuan</code><code>cent</code>分和缺省币种的货币对象
* *
* @param yuan 金额元数 * @param yuan 金额元数0的情况下表示元的部分从分中截取
* @param cent 金额分数 * @param cent 金额分数
*/ */
public Money(long yuan, int cent) { public Money(long yuan, int cent) {
@ -110,15 +110,19 @@ public class Money implements Serializable, Comparable<Money> {
* <p> * <p>
* 创建一个具有金额<code>yuan</code><code>cent</code>分和指定币种的货币对象 * 创建一个具有金额<code>yuan</code><code>cent</code>分和指定币种的货币对象
* *
* @param yuan 金额元数 * @param yuan 金额元数0的情况下表示元的部分从分中截取
* @param cent 金额分数 * @param cent 金额分数
* @param currency 货币单位 * @param currency 货币单位
*/ */
public Money(long yuan, int cent, Currency currency) { public Money(long yuan, int cent, Currency currency) {
this.currency = currency; this.currency = currency;
if(0 == yuan) {
this.cent = cent;
} else{
this.cent = (yuan * getCentFactor()) + (cent % getCentFactor()); this.cent = (yuan * getCentFactor()) + (cent % getCentFactor());
} }
}
/** /**
* 构造器 * 构造器

View File

@ -86,6 +86,12 @@ public class LocalDateTimeUtilTest {
Assert.assertEquals("2020-01-23", localDate.toString()); 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 @Test
public void formatTest() { public void formatTest() {
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56"); final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");

View 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);
}
}