本文整理了Java中java.math.BigDecimal.longValue()
方法的一些代码示例,展示了BigDecimal.longValue()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigDecimal.longValue()
方法的具体详情如下:
包路径:java.math.BigDecimal
类名称:BigDecimal
方法名:longValue
[英]Returns this BigDecimal as an long value. Any fractional part is discarded. If the integral part of this is too big to be represented as an long, then this % 264 is returned.
[中]将此BigDecimal作为长值返回。任何小数部分都将被丢弃。如果该值的整数部分太大,无法表示为long,则返回此%264。
代码示例来源:origin: aws/aws-sdk-java
public static Date parseUnixTimestampInMillis(String dateString) {
if (dateString == null)
return null;
try {
BigDecimal dateValue = new BigDecimal(dateString);
return new Date(dateValue.longValue());
} catch (NumberFormatException nfe) {
throw new SdkClientException("Unable to parse date : "
+ dateString, nfe);
}
}
代码示例来源:origin: prestodb/presto
/**
* Divides the dividend by divisor. Rounding of result occurs
* as per the roundingMode.
*
* @param dividend the dividend
* @param divisor the divisor
* @param roundingMode the desired rounding mode
* @return the division result as per the specified rounding mode
* @throws ArithmeticException if the operation overflows or the divisor is zero
*/
public static long safeDivide(long dividend, long divisor, RoundingMode roundingMode) {
if (dividend == Long.MIN_VALUE && divisor == -1L) {
throw new ArithmeticException("Multiplication overflows a long: " + dividend + " / " + divisor);
}
BigDecimal dividendBigDecimal = new BigDecimal(dividend);
BigDecimal divisorBigDecimal = new BigDecimal(divisor);
return dividendBigDecimal.divide(divisorBigDecimal, roundingMode).longValue();
}
代码示例来源:origin: wildfly/wildfly
@Override
public synchronized long getAverageSessionAliveTime() {
//this method needs to be synchronised to make sure the session count and the total are in sync
if(expiredSessionCount == 0) {
return 0;
}
return new BigDecimal(totalSessionLifetime).divide(BigDecimal.valueOf(expiredSessionCount), MathContext.DECIMAL128).longValue();
}
代码示例来源:origin: knowm/XChange
private static Date convertBigDecimalTimestampToDate(BigDecimal timestamp) {
BigDecimal timestampInMillis = timestamp.multiply(new BigDecimal("1000"));
return new Date(timestampInMillis.longValue());
}
代码示例来源:origin: alibaba/TProfiler
/**
* @param v1
* @param v2
* @return
*/
public static long div(long v1, long v2) {
BigDecimal b1 = new BigDecimal(v1);
BigDecimal b2 = new BigDecimal(v2);
return b1.divide(b2, 0, BigDecimal.ROUND_HALF_UP).longValue();
}
}
代码示例来源:origin: alibaba/TProfiler
/**
* @param v1
* @param v2
* @return
*/
public static long add(long v1, long v2) {
BigDecimal b1 = new BigDecimal(v1);
BigDecimal b2 = new BigDecimal(v2);
return b1.add(b2).longValue();
}
代码示例来源:origin: knowm/XChange
private BigDecimal roundUp(BigDecimal x) {
long n = x.longValue();
return new BigDecimal(x.equals(new BigDecimal(n)) ? n : n + 1);
}
代码示例来源:origin: knowm/XChange
private BigDecimal roundUp(BigDecimal x) {
long n = x.longValue();
return new BigDecimal(x.equals(new BigDecimal(n)) ? n : n + 1);
}
代码示例来源:origin: knowm/XChange
private BigDecimal roundUp(BigDecimal x) {
long n = x.longValue();
return new BigDecimal(x.equals(new BigDecimal(n)) ? n : n + 1);
}
代码示例来源:origin: joda-time/joda-time
/**
* Divides the dividend by divisor. Rounding of result occurs
* as per the roundingMode.
*
* @param dividend the dividend
* @param divisor the divisor
* @param roundingMode the desired rounding mode
* @return the division result as per the specified rounding mode
* @throws ArithmeticException if the operation overflows or the divisor is zero
*/
public static long safeDivide(long dividend, long divisor, RoundingMode roundingMode) {
if (dividend == Long.MIN_VALUE && divisor == -1L) {
throw new ArithmeticException("Multiplication overflows a long: " + dividend + " / " + divisor);
}
BigDecimal dividendBigDecimal = new BigDecimal(dividend);
BigDecimal divisorBigDecimal = new BigDecimal(divisor);
return dividendBigDecimal.divide(divisorBigDecimal, roundingMode).longValue();
}
代码示例来源:origin: JodaOrg/joda-time
/**
* Divides the dividend by divisor. Rounding of result occurs
* as per the roundingMode.
*
* @param dividend the dividend
* @param divisor the divisor
* @param roundingMode the desired rounding mode
* @return the division result as per the specified rounding mode
* @throws ArithmeticException if the operation overflows or the divisor is zero
*/
public static long safeDivide(long dividend, long divisor, RoundingMode roundingMode) {
if (dividend == Long.MIN_VALUE && divisor == -1L) {
throw new ArithmeticException("Multiplication overflows a long: " + dividend + " / " + divisor);
}
BigDecimal dividendBigDecimal = new BigDecimal(dividend);
BigDecimal divisorBigDecimal = new BigDecimal(divisor);
return dividendBigDecimal.divide(divisorBigDecimal, roundingMode).longValue();
}
代码示例来源:origin: stackoverflow.com
BigDecimal bd = new BigDecimal(300000);
NumberFormat formatter = NumberFormat.getInstance(new Locale("en_US"));
System.out.println(formatter.format(bd.longValue()));
代码示例来源:origin: apache/hive
protected BigDecimal trunc(BigDecimal input, int scale) {
BigDecimal output = new BigDecimal(0);
BigDecimal pow = BigDecimal.valueOf(Math.pow(10, Math.abs(scale)));
if (scale >= 0) {
pow = BigDecimal.valueOf(Math.pow(10, scale));
if (scale != 0) {
long longValue = input.multiply(pow).longValue();
output = BigDecimal.valueOf(longValue).divide(pow);
} else {
output = BigDecimal.valueOf(input.longValue());
}
} else {
long longValue2 = input.divide(pow).longValue();
output = BigDecimal.valueOf(longValue2).multiply(pow);
}
return output;
}
代码示例来源:origin: apache/drill
protected BigDecimal trunc(BigDecimal input, int scale) {
BigDecimal output = new BigDecimal(0);
BigDecimal pow = BigDecimal.valueOf(Math.pow(10, Math.abs(scale)));
if (scale >= 0) {
pow = BigDecimal.valueOf(Math.pow(10, scale));
if (scale != 0) {
long longValue = input.multiply(pow).longValue();
output = BigDecimal.valueOf(longValue).divide(pow);
} else {
output = BigDecimal.valueOf(input.longValue());
}
} else {
long longValue2 = input.divide(pow).longValue();
output = BigDecimal.valueOf(longValue2).multiply(pow);
}
return output;
}
代码示例来源:origin: aws/aws-sdk-java
/**
* Parses the given date string returned by the AWS service into a Date
* object.
*/
public static Date parseServiceSpecificDate(String dateString) {
if (dateString == null)
return null;
try {
BigDecimal dateValue = new BigDecimal(dateString);
return new Date(dateValue.scaleByPowerOfTen(
AWS_DATE_MILLI_SECOND_PRECISION).longValue());
} catch (NumberFormatException nfe) {
throw new SdkClientException("Unable to parse date : "
+ dateString, nfe);
}
}
代码示例来源:origin: oracle/helidon
static Function<Object, Object> timeConverter(TimeUnit from) {
switch (from) {
case NANOSECONDS:
return (o) -> String.valueOf(new BigDecimal(String.valueOf(o)).doubleValue() / NANOSECONDS);
case MICROSECONDS:
return (o) -> String.valueOf(new BigDecimal(String.valueOf(o)).doubleValue() / MICROSECONDS);
case MILLISECONDS:
return (o) -> String.valueOf(new BigDecimal(String.valueOf(o)).doubleValue() / MILLISECONDS);
case SECONDS:
return String::valueOf;
default:
return (o) -> String.valueOf(TimeUnit.SECONDS.convert(new BigDecimal(String.valueOf(o)).longValue(), from));
}
}
}
代码示例来源:origin: apache/incubator-pinot
/**
* Convert human readable datasize to bytes
* @param val string to parse
* @return returns -1 in case of invalid value
*/
public static long toBytes(@Nullable String val) {
if (val == null) {
return -1;
}
Matcher matcher = STORAGE_VAL_PATTERN.matcher(val);
if (!matcher.matches()) {
return -1;
}
String number = matcher.group(1);
String unit = matcher.group(2);
if (unit == null) {
unit = "B";
}
long multiplier = MULTIPLIER.get(unit.toUpperCase());
BigDecimal bytes = new BigDecimal(number);
return bytes.multiply(BigDecimal.valueOf(multiplier)).longValue();
}
代码示例来源:origin: json-path/JsonPath
private static Number unwrapNumber(final Number n) {
Number unwrapped;
if (!isPrimitiveNumber(n)) {
BigDecimal bigDecimal = new BigDecimal(n.toString());
if (bigDecimal.scale() <= 0) {
if (bigDecimal.compareTo(new BigDecimal(Integer.MAX_VALUE)) <= 0) {
unwrapped = bigDecimal.intValue();
} else if (bigDecimal.compareTo(new BigDecimal(Long.MAX_VALUE)) <= 0){
unwrapped = bigDecimal.longValue();
} else {
unwrapped = bigDecimal;
}
} else {
final double doubleValue = bigDecimal.doubleValue();
if (BigDecimal.valueOf(doubleValue).compareTo(bigDecimal) != 0) {
unwrapped = bigDecimal;
} else {
unwrapped = doubleValue;
}
}
} else {
unwrapped = n;
}
return unwrapped;
}
代码示例来源:origin: apache/hive
public static Timestamp decimalToTimestamp(HiveDecimalV1 dec) {
try {
BigDecimal nanoInstant = dec.bigDecimalValue().multiply(BILLION_BIG_DECIMAL);
int nanos = nanoInstant.remainder(BILLION_BIG_DECIMAL).intValue();
if (nanos < 0) {
nanos += 1000000000;
}
long seconds =
nanoInstant.subtract(new BigDecimal(nanos)).divide(BILLION_BIG_DECIMAL).longValue();
Timestamp t = new Timestamp(seconds * 1000);
t.setNanos(nanos);
return t;
} catch (NumberFormatException nfe) {
return null;
} catch (IllegalArgumentException iae) {
return null;
}
}
代码示例来源:origin: apache/hive
public static Timestamp decimalToTimestamp(HiveDecimalV1 dec) {
try {
BigDecimal nanoInstant = dec.bigDecimalValue().multiply(BILLION_BIG_DECIMAL);
int nanos = nanoInstant.remainder(BILLION_BIG_DECIMAL).intValue();
if (nanos < 0) {
nanos += 1000000000;
}
long seconds =
nanoInstant.subtract(new BigDecimal(nanos)).divide(BILLION_BIG_DECIMAL).longValue();
return Timestamp.ofEpochSecond(seconds, nanos);
} catch (IllegalArgumentException | DateTimeException nfe) {
return null;
}
}
内容来源于网络,如有侵权,请联系作者删除!