本文整理汇总了Java中java.math.BigDecimal.valueOf方法的典型用法代码示例。如果您正苦于以下问题:Java BigDecimal.valueOf方法的具体用法?Java BigDecimal.valueOf怎么用?Java BigDecimal.valueOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigDecimal
的用法示例。
在下文中一共展示了BigDecimal.valueOf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: castToDecimal
import java.math.BigDecimal; //导入方法依赖的package包/类
public static BigDecimal castToDecimal(Object val) {
if (val == null) {
return null;
}
if (val instanceof BigDecimal) {
return (BigDecimal) val;
}
if (val instanceof String) {
return new BigDecimal((String) val);
}
if (val instanceof Float) {
return new BigDecimal((Float) val);
}
if (val instanceof Double) {
return new BigDecimal((Double) val);
}
return BigDecimal.valueOf(((Number) val).longValue());
}
示例2: sendFunds
import java.math.BigDecimal; //导入方法依赖的package包/类
private void sendFunds(Greeter contract) throws Exception {
System.out.println("// Send 0.05 Ethers to contract");
// trasfer ether to contract account
String contractAddress = contract.getContractAddress();
BigDecimal amountEther = BigDecimal.valueOf(0.05);
BigInteger amountWei = Web3jUtils.etherToWei(amountEther);
Web3jUtils.transferFromCoinbaseAndWait(web3j, contractAddress, amountWei);
// check current # of deposits and balance
Uint256 deposits = contract
.deposits()
.get();
System.out.println("Contract address balance (after funding): " + Web3jUtils.weiToEther(Web3jUtils.getBalanceWei(web3j, contractAddress)));
System.out.println("Contract.deposits(): " + deposits.getValue() + "\n");
}
示例3: addCurrency_EUR
import java.math.BigDecimal; //导入方法依赖的package包/类
private void addCurrency_EUR(ResellerRevenueShareResult result) {
Currency currency = new Currency("EUR");
result.getCurrency().add(currency);
// add supplier
Supplier supplier = new Supplier();
currency.addSupplier(supplier);
BigDecimal[] subscriptionRevenues = new BigDecimal[] {
BigDecimal.valueOf(30), BigDecimal.valueOf(2.55),
BigDecimal.valueOf(2.045) };
// total revenue : 30 + 2.55 + 2.05 = 34.6
addService(supplier, subscriptionRevenues);
BigDecimal[] subscriptionRevenues2 = new BigDecimal[] { BigDecimal
.valueOf(4) };
// total revenue : 4
addService(supplier, subscriptionRevenues2);
}
示例4: testBillingWithSteppedPricesForPriceModelStep0
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* Billing test for price model with stepped price.
*
* @throws Exception
*/
@Test
public void testBillingWithSteppedPricesForPriceModelStep0()
throws Exception {
int numUser = 4;
BigDecimal expectedPrice = new BigDecimal("1258.34");
final Long[] limitArrayForDifferentAssignmentTime = new Long[] {
Long.valueOf(2), Long.valueOf(3), null };
BigDecimal[] priceArray = { BD500, BD400, BD300 };
long[] freeAmountArray = { 0, 2, 3 };
BigDecimal[] additionalPriceArray = { BigDecimal.ZERO, BD1000,
BigDecimal.valueOf(1400) };
BigDecimal[] stepAmountArray = { BD1000, new BigDecimal("257.34") };
testBillingWithSteppedPricesForPriceModel(0, numUser, expectedPrice,
true, limitArrayForDifferentAssignmentTime, priceArray,
freeAmountArray, additionalPriceArray, stepAmountArray);
xmlValidator.validateBillingResultXML();
}
示例5: bigDecValue
import java.math.BigDecimal; //导入方法依赖的package包/类
private BigDecimal bigDecValue(Object source) throws NumberFormatException {
if (source == null){
return BigDecimal.valueOf(0L);
}
Class<? extends Object> c = source.getClass();
if (c == BigDecimal.class){
return (BigDecimal) source;
}
if (c == BigInteger.class){
return new BigDecimal((BigInteger) source);
}
if (c.getSuperclass() == Number.class){
return new BigDecimal(((Number) source).doubleValue());
}
if (c == Boolean.class){
return BigDecimal.valueOf(((Boolean) source).booleanValue() ? 1 : 0);
}
if (c == Character.class){
return BigDecimal.valueOf(((Character) source).charValue());
}
String s = stringValue(source, true);
return (s.length() == 0) ? BigDecimal.valueOf(0L) : new BigDecimal(s);
}
示例6: getPayable
import java.math.BigDecimal; //导入方法依赖的package包/类
@Override
public BigDecimal getPayable() {
if (py != null) {
return py;
}
return BigDecimal.valueOf(0);
}
示例7: toBigDecimal
import java.math.BigDecimal; //导入方法依赖的package包/类
public static BigDecimal toBigDecimal(Number number) {
if (number instanceof BigDecimal) {
return (BigDecimal) number;
}
if (number instanceof Integer) {
return new BigDecimal(number.intValue());
}
if (number instanceof Double || number instanceof Float) {// BigDecimal float 也是转double
return new BigDecimal(number.doubleValue());
}
if (number instanceof Long) {
return BigDecimal.valueOf(number.longValue());
}
return new BigDecimal(number.toString());
}
示例8: getConvertedPrice
import java.math.BigDecimal; //导入方法依赖的package包/类
private String getConvertedPrice(String input, int decimalPlacesSetting) {
String convertedPrice = input;
if (convertedPrice != null && convertedPrice.length() > 0) {
BigDecimal result = BigDecimal.valueOf(Long.parseLong(input),
decimalPlacesSetting);
convertedPrice = result.toPlainString();
}
return convertedPrice;
}
示例9: validateSteppedPrice_negativePrice
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* Test for validation of stepped price. Negative price. Exception is
* expected.
*/
@Test(expected = ValidationException.class)
public void validateSteppedPrice_negativePrice() throws Exception {
// given
BigDecimal price = BigDecimal.valueOf(-100L);
VOSteppedPrice voSteppedPrice = new VOSteppedPrice();
voSteppedPrice.setPrice(price);
// when
SteppedPriceAssembler.validateSteppedPrice(voSteppedPrice);
}
示例10: unwrap
import java.math.BigDecimal; //导入方法依赖的package包/类
@SuppressWarnings({ "unchecked" })
@Override
public <X> X unwrap(Double value, Class<X> type, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( Double.class.isAssignableFrom( type ) ) {
return (X) value;
}
if ( Byte.class.isAssignableFrom( type ) ) {
return (X) Byte.valueOf( value.byteValue() );
}
if ( Short.class.isAssignableFrom( type ) ) {
return (X) Short.valueOf( value.shortValue() );
}
if ( Integer.class.isAssignableFrom( type ) ) {
return (X) Integer.valueOf( value.intValue() );
}
if ( Long.class.isAssignableFrom( type ) ) {
return (X) Long.valueOf( value.longValue() );
}
if ( Float.class.isAssignableFrom( type ) ) {
return (X) Float.valueOf( value.floatValue() );
}
if ( BigInteger.class.isAssignableFrom( type ) ) {
return (X) BigInteger.valueOf( value.longValue() );
}
if ( BigDecimal.class.isAssignableFrom( type ) ) {
return (X) BigDecimal.valueOf( value );
}
if ( String.class.isAssignableFrom( type ) ) {
return (X) value.toString();
}
throw unknownUnwrap( type );
}
示例11: testSubnormalPowers
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* For each subnormal power of two, test at boundaries of
* region that should convert to that value.
*/
private static void testSubnormalPowers() {
boolean failed = false;
BigDecimal TWO = BigDecimal.valueOf(2);
// An ulp is the same for all subnormal values
BigDecimal ulp_BD = new BigDecimal(Double.MIN_VALUE);
// Test subnormal powers of two (except Double.MIN_VALUE)
for(int i = -1073; i <= -1022; i++) {
double d = Math.scalb(1.0, i);
/*
* The region [d - ulp/2, d + ulp/2] should round to d.
*/
BigDecimal d_BD = new BigDecimal(d);
BigDecimal lowerBound = d_BD.subtract(ulp_BD.divide(TWO));
BigDecimal upperBound = d_BD.add(ulp_BD.divide(TWO));
double convertedLowerBound = Double.parseDouble(lowerBound.toString());
double convertedUpperBound = Double.parseDouble(upperBound.toString());
if (convertedLowerBound != d) {
failed = true;
System.out.printf("2^%d lowerBound converts as %a %s%n",
i, convertedLowerBound, lowerBound);
}
if (convertedUpperBound != d) {
failed = true;
System.out.printf("2^%d upperBound converts as %a %s%n",
i, convertedUpperBound, upperBound);
}
}
/*
* Double.MIN_VALUE
* The region ]0.5*Double.MIN_VALUE, 1.5*Double.MIN_VALUE[ should round to Double.MIN_VALUE .
*/
BigDecimal minValue = new BigDecimal(Double.MIN_VALUE);
if (Double.parseDouble(minValue.multiply(new BigDecimal(0.5)).toString()) != 0.0) {
failed = true;
System.out.printf("0.5*MIN_VALUE doesn't convert 0%n");
}
if (Double.parseDouble(minValue.multiply(new BigDecimal(0.50000000001)).toString()) != Double.MIN_VALUE) {
failed = true;
System.out.printf("0.50000000001*MIN_VALUE doesn't convert to MIN_VALUE%n");
}
if (Double.parseDouble(minValue.multiply(new BigDecimal(1.49999999999)).toString()) != Double.MIN_VALUE) {
failed = true;
System.out.printf("1.49999999999*MIN_VALUE doesn't convert to MIN_VALUE%n");
}
if (Double.parseDouble(minValue.multiply(new BigDecimal(1.5)).toString()) != 2*Double.MIN_VALUE) {
failed = true;
System.out.printf("1.5*MIN_VALUE doesn't convert to 2*MIN_VALUE%n");
}
if (failed)
throw new RuntimeException("Inconsistent conversion");
}
示例12: calculateMbps
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* Compute a throughput rate in MB/s.
* @param rows Number of records consumed.
* @param timeMs Time taken in milliseconds.
* @return String value with label, ie '123.76 MB/s'
*/
private static String calculateMbps(int rows, long timeMs, final int valueSize, int columns) {
BigDecimal rowSize = BigDecimal.valueOf(ROW_LENGTH +
((valueSize + FAMILY_NAME.length + COLUMN_ZERO.length) * columns));
BigDecimal mbps = BigDecimal.valueOf(rows).multiply(rowSize, CXT)
.divide(BigDecimal.valueOf(timeMs), CXT).multiply(MS_PER_SEC, CXT)
.divide(BYTES_PER_MB, CXT);
return FMT.format(mbps) + " MB/s";
}
示例13: testGetStepCost_3
import java.math.BigDecimal; //导入方法依赖的package包/类
@Test
public void testGetStepCost_3() {
Long[] limitArray = { L1, L2, L3, L4, L5, null };
BigDecimal[] priceArray = { BD10, BD9, BD8, BD7, BD6, BD5 };
Long[] freeEntityCountArray = { L0, L1, L2, L3, L4, L5 };
BigDecimal[] additionalPriceArray = { BigDecimal.ZERO, BD10,
BigDecimal.valueOf(19), BigDecimal.valueOf(27),
BigDecimal.valueOf(34), BigDecimal.valueOf(40) };
List<SteppedPriceData> steppedPricesList = getSteppedPricesList(
limitArray, priceArray, freeEntityCountArray,
additionalPriceArray);
SteppedPriceDetail steppedpriceDetail = initSteppedPriceDetail(steppedPricesList);
BigDecimal value = new BigDecimal(10);
SteppedPriceDetail stepCost = calculator.calculateStepCost(
steppedpriceDetail, value);
BigDecimal actualCost = stepCost.getNormalizedCost();
checkEquals(new BigDecimal(65), actualCost, Numbers.BIGDECIMAL_SCALE);
List<SteppedPriceData> priceData = stepCost.getPriceData();
BigDecimal[] entityCount = { BigDecimal.ONE, BigDecimal.ONE,
BigDecimal.ONE, BigDecimal.ONE, BigDecimal.ONE, BD5 };
BigDecimal[] stepAmount = { BD10, BD9, BD8, BD7, BD6,
BigDecimal.valueOf(25) };
for (int i = 0; i < limitArray.length; i++) {
SteppedPriceData data = priceData.get(i);
checkEquals(additionalPriceArray[i], data.getAdditionalPrice(),
Numbers.BIGDECIMAL_SCALE);
Long limit = data.getLimit();
assertEquals(limitArray[i], limit);
assertEquals(freeEntityCountArray[i].longValue(),
data.getFreeEntityCount());
checkEquals(stepAmount[i], data.getStepAmount(),
Numbers.BIGDECIMAL_SCALE);
checkEquals(priceArray[i], data.getBasePrice(),
Numbers.BIGDECIMAL_SCALE);
checkEquals(entityCount[i], data.getStepEntityCount(),
Numbers.BIGDECIMAL_SCALE);
}
}
示例14: getSeconds
import java.math.BigDecimal; //导入方法依赖的package包/类
/**
* @return result of adding second and fractional second field
*/
private BigDecimal getSeconds() {
if (second == DatatypeConstants.FIELD_UNDEFINED) {
return DECIMAL_ZERO;
}
BigDecimal result = BigDecimal.valueOf((long) second);
if (fractionalSecond != null) {
return result.add(fractionalSecond);
} else {
return result;
}
}
示例15: bigDecimalValue
import java.math.BigDecimal; //导入方法依赖的package包/类
@Override
public BigDecimal bigDecimalValue(Double value) {
return BigDecimal.valueOf(value);
}