List of usage examples for java.math BigDecimal valueOf
public static BigDecimal valueOf(double val)
From source file:com.aegiswallet.utils.WalletUtils.java
public static String getWalletCurrencyValue(Context context, SharedPreferences prefs, BigInteger balance) { String result = ""; File file = context.getApplicationContext().getFileStreamPath(Constants.BLOCKCHAIN_CURRENCY_FILE_NAME); if (file.exists()) { JSONObject jsonObject = BasicUtils.parseJSONData(context, Constants.BLOCKCHAIN_CURRENCY_FILE_NAME); try {/*from w w w .j av a 2 s . co m*/ String balanceInBTC = balance.toString(); if (balance.longValue() > 0) balanceInBTC = BasicUtils.formatValue(balance, Constants.BTC_MAX_PRECISION, 0); BigDecimal formattedBalance = new BigDecimal(balanceInBTC); if (jsonObject != null) { JSONObject newObject = jsonObject .getJSONObject(prefs.getString(Constants.CURRENCY_PREF_KEY, null)); Double doubleVal = newObject.getDouble("last"); BigDecimal decimal = BigDecimal.valueOf(doubleVal); result = newObject.getString("symbol") + decimal.multiply(formattedBalance).setScale(2, RoundingMode.HALF_EVEN).toString(); } } catch (JSONException e) { Log.e("Wallet Utils", "JSON Exception " + e.getMessage()); } } return result; }
From source file:com.bitsofproof.example.Simple.java
public static String printBit(long n) { BigDecimal xbt = BigDecimal.valueOf(n).divide(BigDecimal.valueOf(100)); return NumberFormat.getNumberInstance().format(xbt) + " bit"; }
From source file:fragment.web.AbstractProductsControllerTest.java
@Test public void testCreatProductWithRunningVM() throws Exception { User user = userDAO.find("3"); user.setProfile(profileDAO.find("7")); userDAO.save(user);/* www.j a v a2 s. co m*/ asUser(user); String[] discriminators = { "ServiceOfferingUUID", "TemplateUUID", "HypervisorType", "RAMSize", "Speed", "GuestOSName" }; for (int i = 0; i < discriminators.length; i++) { ServiceInstance serviceInstance = serviceInstanceDAO.find("1"); ServiceUsageType serviceUsageType = createDiscriminator(serviceInstance, "RUNNING_VM", discriminators[i]); int beforeProductsCount = productsService.getProductsCount(); Product obtainedProduct = productCreation("Code" + i, true, serviceUsageType.getId(), 1L, "1.00", true, discriminators[i], "Test_Discri" + i); Assert.assertNotNull(obtainedProduct); Assert.assertEquals(obtainedProduct.getName(), "New_Prod"); int afterProductCount = productsService.getProductsCount(); Assert.assertEquals(beforeProductsCount + 1, afterProductCount); List<ProductCharge> obtainedProductCharges = productsService.getProductCharges(obtainedProduct, obtainedProduct.getCreatedAt()); for (ProductCharge pc : obtainedProductCharges) { Assert.assertEquals(BigDecimal.valueOf(100), pc.getPrice()); Assert.assertEquals(currencyValueDAO.find(1L), pc.getCurrencyValue()); } Map<ServiceUsageType, List<Product>> productUsageMap = productsService .getProductsByUsageType(revisionDAO.getCurrentRevision(null)); List<Product> prodlist = productUsageMap.get(serviceUsageType); Assert.assertEquals(true, prodlist.contains(obtainedProduct)); List<MediationRule> mrlist = mediationRuleDAO.findAllMediationRules(serviceUsageType, serviceInstance, revisionDAO.getCurrentRevision(null)); for (MediationRule mr : mrlist) { if (mr.getProduct().equals(obtainedProduct)) { Set<MediationRuleDiscriminator> mrdset = mr.getMediationRuleDiscriminators(); for (MediationRuleDiscriminator mrd : mrdset) { Assert.assertEquals("Test_Discri" + i, mrd.getDiscriminatorValueDisplayName()); } } } } }
From source file:org.excalibur.driver.google.compute.GoogleCompute.java
public List<InstanceType> listInstanceTypes() { List<InstanceType> instanceTypes = new ArrayList<InstanceType>(); Map<String, MachineTypesScopedList> machineTypesByZone = this.compute.machineTypes() .aggregatedList(this.credentials_.getProject()).execute().getItems(); for (String zone : machineTypesByZone.keySet()) { List<MachineType> machineTypesOfZone = machineTypesByZone.get(zone).getMachineTypes(); if (machineTypesOfZone != null) { for (MachineType type : machineTypesOfZone) { InstanceType instance = new InstanceType() .setConfiguration(new InstanceHardwareConfiguration() .setNumberOfCores(type.getGuestCpus()) .setRamMemorySizeGb(BigDecimal.valueOf(type.getMemoryMb().doubleValue() / 1024) .setScale(2, java.math.RoundingMode.HALF_EVEN).doubleValue()) .setDiskSizeGb(type.getImageSpaceGb().longValue())) .setId(type.getId().intValue()).setName(type.getName()); if (!instanceTypes.contains(instance)) { instanceTypes.add(instance); }//from w w w . j av a 2 s . c om } } } return ImmutableList.copyOf(instanceTypes); }
From source file:com.streamsets.pipeline.lib.parser.excel.TestWorkbookParser.java
@Test public void testParseHandlesMultipleSheets() throws IOException, InvalidFormatException, DataParserException { Workbook workbook = createWorkbook("/excel/TestMultipleSheets.xlsx"); WorkbookParser parser = new WorkbookParser(settingsWithHeader, getContext(), workbook, "Sheet1::0"); // column header prefix, row value multiplier List<Pair<String, Integer>> sheetParameters = Arrays.asList(Pair.of("column", 1), Pair.of("header", 10)); for (int sheet = 1; sheet <= sheetParameters.size(); sheet++) { for (int row = 1; row <= 2; row++) { Record parsedRow = parser.parse(); LinkedHashMap<String, Field> contentMap = new LinkedHashMap<>(); String columnPrefix = sheetParameters.get(sheet - 1).getLeft(); Integer valueMultiplier = sheetParameters.get(sheet - 1).getRight(); for (int column = 1; column <= 5; column++) { contentMap.put(columnPrefix + column, Field.create(BigDecimal.valueOf(column * valueMultiplier))); }/*from w w w .j a va 2 s . co m*/ Field expectedRow = Field.createListMap(contentMap); assertEquals(String.format("Parsed value for sheet %1d, row %2d did not match expected value", sheet, row), expectedRow, parsedRow.get()); } } }
From source file:com.jeans.iservlet.action.admin.DataImportAction.java
/** * ?//from w w w .ja v a 2s . c o m * * @return * @throws Exception */ @Action(value = "ci-import", results = { @Result(name = SUCCESS, type = "json", params = { "root", "results", "contentType", "text/plain", "encoding", "UTF-8" }) }) public String uploadCIData() throws Exception { if (!checkDataFile()) { return SUCCESS; } int hardCount = 0, softCount = 0; try (Workbook workBook = WorkbookFactory.create(data)) { Sheet hardSheet = workBook.getSheet(""); Sheet softSheet = workBook.getSheet(""); if (null == hardSheet || null == softSheet) { results.put("code", 4); results.put("tip", "????Sheet"); return SUCCESS; } Company comp = getCurrentCompany(); ExcelUtils.setNumberFormat("#"); SimpleDateFormat sdf = new SimpleDateFormat("yyyymm"); // hardSheet: 1?015?160? int last = hardSheet.getLastRowNum(); for (int rn = 1; rn <= last; rn++) { Row r = hardSheet.getRow(rn); // ??""??? String flag = StringUtils .trim(ExcelUtils.getCellValueAsString(r.getCell(0, Row.RETURN_BLANK_AS_NULL))); // ???name? String name = StringUtils .trim(ExcelUtils.getCellValueAsString(r.getCell(3, Row.RETURN_BLANK_AS_NULL))); if (!"".equals(flag) || StringUtils.isBlank(name)) { continue; } Map<String, Object> hardware = new HashMap<String, Object>(); hardware.put("company", comp); hardware.put("type", AssetConstants.HARDWARE_ASSET); hardware.put("name", name); hardware.put("code", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(1, Row.RETURN_BLANK_AS_NULL)))); hardware.put("catalog", parseAssetCatalog( StringUtils.trim( ExcelUtils.getCellValueAsString(r.getCell(2, Row.RETURN_BLANK_AS_NULL))), AssetConstants.HARDWARE_ASSET)); hardware.put("vendor", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(4, Row.RETURN_BLANK_AS_NULL)))); hardware.put("modelOrVersion", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(5, Row.RETURN_BLANK_AS_NULL)))); hardware.put("assetUsage", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(6, Row.RETURN_BLANK_AS_NULL)))); hardware.put("purchaseTime", ExcelUtils.getCellValueAsDate(r.getCell(7, Row.RETURN_BLANK_AS_NULL), sdf)); try { double q = (double) ExcelUtils.getCellValue(r.getCell(8, Row.RETURN_BLANK_AS_NULL)); if (q < 1) { hardware.put("quantity", 1); } else { hardware.put("quantity", (int) Math.round(q)); } } catch (Exception e) { hardware.put("quantity", 1); } try { hardware.put("cost", BigDecimal .valueOf((double) ExcelUtils.getCellValue(r.getCell(9, Row.RETURN_BLANK_AS_NULL)))); } catch (Exception e) { hardware.put("cost", new BigDecimal(0)); } hardware.put("state", AssetConstants.IDLE); hardware.put("sn", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(10, Row.RETURN_BLANK_AS_NULL)))); hardware.put("configuration", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(11, Row.RETURN_BLANK_AS_NULL)))); hardware.put("warranty", AssetConstants.IMPLIED_WARRANTY); hardware.put("location", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(12, Row.RETURN_BLANK_AS_NULL)))); hardware.put("ip", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(13, Row.RETURN_BLANK_AS_NULL)))); hardware.put("importance", AssetConstants.GENERAL_DEGREE); hardware.put("owner", null); hardware.put("comment", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(14, Row.RETURN_BLANK_AS_NULL)))); hardware.put("financialCode", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(15, Row.RETURN_BLANK_AS_NULL)))); assetService.newAsset(hardware); hardCount++; } // softSheet: 1?012?130? last = softSheet.getLastRowNum(); for (int rn = 1; rn <= last; rn++) { Row r = softSheet.getRow(rn); // ??""??? String flag = StringUtils .trim(ExcelUtils.getCellValueAsString(r.getCell(0, Row.RETURN_BLANK_AS_NULL))); // ???name? String name = StringUtils .trim(ExcelUtils.getCellValueAsString(r.getCell(2, Row.RETURN_BLANK_AS_NULL))); if (!"".equals(flag) || StringUtils.isBlank(name)) { continue; } if (StringUtils.isBlank(name)) continue; Map<String, Object> software = new HashMap<String, Object>(); software.put("company", comp); software.put("type", AssetConstants.SOFTWARE_ASSET); software.put("name", name); software.put("catalog", parseAssetCatalog( StringUtils.trim( ExcelUtils.getCellValueAsString(r.getCell(1, Row.RETURN_BLANK_AS_NULL))), AssetConstants.SOFTWARE_ASSET)); software.put("vendor", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(3, Row.RETURN_BLANK_AS_NULL)))); software.put("modelOrVersion", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(4, Row.RETURN_BLANK_AS_NULL)))); try { double q = (double) ExcelUtils.getCellValue(r.getCell(5, Row.RETURN_BLANK_AS_NULL)); if (q < 1) { software.put("quantity", 1); } else { software.put("quantity", (int) Math.round(q)); } } catch (Exception e) { software.put("quantity", 1); } try { software.put("cost", BigDecimal .valueOf((double) ExcelUtils.getCellValue(r.getCell(6, Row.RETURN_BLANK_AS_NULL)))); } catch (Exception e) { software.put("cost", new BigDecimal(0)); } software.put("purchaseTime", ExcelUtils.getCellValueAsDate(r.getCell(7, Row.RETURN_BLANK_AS_NULL), sdf)); software.put("assetUsage", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(8, Row.RETURN_BLANK_AS_NULL)))); software.put("state", AssetConstants.IN_USE); software.put("softwareType", parseSoftwareType( StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(9, Row.RETURN_BLANK_AS_NULL))))); software.put("license", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(10, Row.RETURN_BLANK_AS_NULL)))); software.put("expiredTime", ExcelUtils.getCellValueAsDate(r.getCell(11, Row.RETURN_BLANK_AS_NULL), sdf)); software.put("comment", StringUtils.trim(ExcelUtils.getCellValueAsString(r.getCell(12, Row.RETURN_BLANK_AS_NULL)))); assetService.newAsset(software); softCount++; } results.put("code", 0); results.put("tip", "???" + hardCount + "" + softCount + ""); log("?CI??" + hardCount + "" + softCount + ""); return SUCCESS; } catch (Exception e) { log(e); results.put("code", 4); results.put("tip", "???" + hardCount + "" + softCount + ""); log("?CI??" + hardCount + "" + softCount + ""); return SUCCESS; } }
From source file:com.norconex.collector.core.crawler.AbstractCrawler.java
private void setProgress(JobStatusUpdater statusUpdater, ICrawlDataStore db) { int queued = db.getQueueSize(); int processed = processedCount; int total = queued + processed; double progress = 0; if (total != 0) { progress = BigDecimal.valueOf(processed) .divide(BigDecimal.valueOf(total), DOUBLE_PROGRESS_SCALE, RoundingMode.DOWN).doubleValue(); }//from ww w.j av a2 s .c o m statusUpdater.setProgress(progress); statusUpdater.setNote(NumberFormat.getIntegerInstance().format(processed) + " references processed out of " + NumberFormat.getIntegerInstance().format(total)); if (LOG.isInfoEnabled()) { long currentTime = System.currentTimeMillis(); if (currentTime - lastStatusLoggingTime > STATUS_LOGGING_INTERVAL) { lastStatusLoggingTime = currentTime; int percent = BigDecimal.valueOf(progress).movePointLeft(DOUBLE_PERCENT_SCALE).intValue(); LOG.info( getId() + ": " + percent + "% completed (" + processed + " processed/" + total + " total)"); } } }
From source file:dk.clanie.bitcoin.client.BitcoindClientIntegrationTest.java
@Test public void testMove() throws Exception { BooleanResponse move = bc.move("clanie", "cn@cn-consult", BigDecimal.valueOf(0.1d), null, null); print(move);/* www .jav a 2s . co m*/ }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#ceil(java.lang.Object)}. *///w w w . j a v a 2s.c o m @Test public void testCeilObject() { assertEquals("null", null, ceil((Object) null)); assertEquals("null", null, ceil("")); assertEquals("null", null, ceil("not a number.")); assertEquals("PI: Float: ", 4f, ceil("3.141592653589793f")); assertEquals("PI: Double: ", 4d, ceil("3.141592653589793d")); assertEquals("PI: BigDecimal: ", BigDecimal.valueOf(Math.PI).setScale(0, RoundingMode.CEILING), ceil((Object) BigDecimal.valueOf(Math.PI))); }
From source file:io.coala.time.TimeSpan.java
/** * @param multiplier the {@link Dimensionless} multiplier {@link Measure} * @return a new {@link TimeSpan}//from www . jav a2s. c om */ @SuppressWarnings("unchecked") public TimeSpan multiply(final Measurable<Dimensionless> multiplier) { return multiply(multiplier instanceof DecimalMeasure ? MeasureUtil.toUnit((DecimalMeasure) multiplier, Unit.ONE).getValue() : BigDecimal.valueOf(multiplier.doubleValue(Unit.ONE))); }