List of usage examples for java.math RoundingMode UP
RoundingMode UP
To view the source code for java.math RoundingMode UP.
Click Source Link
From source file:Main.java
public static void main(String[] args) { BigDecimal bg1 = new BigDecimal("123.12345678"); // set scale of bg1 to 2 in bg2 // 0 specifies ROUND_UP BigDecimal bg2 = bg1.setScale(2, RoundingMode.UP); String str = bg1 + " after changing the scale to 2 and rounding is " + bg2; System.out.println(str);/*from w w w. j a v a 2 s . c o m*/ }
From source file:Main.java
public static void main(String[] args) { BigDecimal bg1 = new BigDecimal("16"); BigDecimal bg2 = new BigDecimal("3"); // divide bg1 with bg2 rounding up BigDecimal bg3 = bg1.divide(bg2, RoundingMode.UP); System.out.println(bg3);/*from ww w . j a v a 2 s . com*/ }
From source file:Main.java
/** * Formats decimal number for specified locale * * @param v Value to format/* ww w . j a v a 2s . com*/ * @param locale Locale * @return Formatted string */ public static String formatBigDecimal(BigDecimal v, Locale locale) { NumberFormat numberFormat = NumberFormat.getInstance(locale); int scale = 2; if (v.intValue() >= 100) { scale = 1; } return numberFormat.format(v.setScale(scale, RoundingMode.UP)); }
From source file:ws.moor.bt.util.ByteUtil.java
public static int distance(byte[] a, byte[] b) { byte[] result = xor(a, b); if (result.length == 0) { result = new byte[] { 1 }; }/* ww w . ja v a 2s . c o m*/ result = ArrayUtil.append(new byte[] { 0 }, result); BigDecimal number = new BigDecimal(new BigInteger(result)); if (number.equals(BigDecimal.ZERO)) { number = BigDecimal.ONE; } double clear = BigDecimal.valueOf(2).pow(a.length * 8).divide(number, 4, RoundingMode.UP).doubleValue(); return (int) (100.0 * Math.log(clear) / Math.log(2.0) / a.length); }
From source file:org.jfree.chart.demo.Graphic.java
public ChartPanel get_ChartPanel(int width, int height) { chr = new ChartPanel(chart); chr.setBackground(Color.blue); chr.setBounds(5, 5, width - 5, height - 10); chr.setVisible(true);/* www . ja v a 2s .c o m*/ chr.setMouseWheelEnabled(true); chr.addChartMouseListener(new ChartMouseListener() { public void chartMouseMoved(ChartMouseEvent chartmouseevent) { } public void chartMouseClicked(ChartMouseEvent chartmouseevent) { SwingUtilities.invokeLater(new Runnable() { public void run() { XYPlot xyplot = (XYPlot) chr.getChart().getPlot(); xyplot.clearAnnotations(); double x, y; x = new BigDecimal(xyplot.getDomainCrosshairValue()).setScale(3, RoundingMode.UP) .doubleValue(); y = new BigDecimal(xyplot.getRangeCrosshairValue()).setScale(3, RoundingMode.UP) .doubleValue(); XYTextAnnotation annotation = new XYTextAnnotation("(" + x + ", " + y + ")", new BigDecimal(x).setScale(3, RoundingMode.UP).doubleValue(), y); annotation.setFont(new Font("serif", Font.BOLD, 15)); annotation.setTextAnchor(TextAnchor.BOTTOM_CENTER); xyplot.addAnnotation(annotation); } }); } }); return chr; }
From source file:org.jfree.chart.demo.bottom_slider.java
/** * Create the panel.//from w w w . j a va2 s. c o m */ public bottom_slider(Color color, ArrayList<JFreeChart> chart, final Graphic graph, int num) { setBounds(5, 380, 740, 100); setLayout(null); charts = chart; plot = charts.get(num).getXYPlot(); table = new JTable( new Object[][] { { "Slider", "X", "Y", "X1-X2", "Y1-Y2" }, { "Slider1", null, null, null, null }, { "Slider2", null, null, null, null } }, new String[] { "Slider", "X", "Y", "X1-X2", "Y1-Y2" }); setBackground(color); table.setBounds(255, 22, 485, 48); add(table); slider_2 = new JSlider(); slider_2.setBounds(5, 22, 250, 20); slider_2.setBackground(color); slider_2.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { try { int value = slider_2.getValue(); ValueAxis domainAxis = plot.getDomainAxis(); Range range = domainAxis.getRange(); X1 = domainAxis.getLowerBound() + (value / 100.0) * range.getLength(); int i = 0; for (; i < graph.mas_x.size(); i++) { // System.out.println("X"+ Arrays.toString(graph.mas_x.toArray())); if (graph.mas_x.get(i).doubleValue() <= X1) { } else { break; } } Y1 = graph.mas_y.get(i).doubleValue(); } catch (Exception e1) { if (e.toString() == "java.lang.IndexOutOfBoundsException") Y1 = graph.mas_y.get(graph.mas_y.size() - 1).doubleValue(); } plot.removeDomainMarker(distanceTiers2); distanceTiers2 = new ValueMarker(X1); distanceTiers2.setPaint(Color.BLACK); plot.addDomainMarker(distanceTiers2); table.setValueAt("" + new BigDecimal(X1).setScale(3, RoundingMode.UP).doubleValue(), 1, 1); table.setValueAt("" + new BigDecimal(Y1).setScale(3, RoundingMode.UP).doubleValue(), 1, 2); table.setValueAt("" + new BigDecimal(X2).setScale(3, RoundingMode.UP).doubleValue(), 2, 1); table.setValueAt("" + new BigDecimal(Y2).setScale(3, RoundingMode.UP).doubleValue(), 2, 2); table.setValueAt("" + new BigDecimal(Math.abs(X2 - X1)).setScale(3, RoundingMode.UP).doubleValue(), 2, 3); table.setValueAt("" + new BigDecimal(Math.abs(X2 - X1)).setScale(3, RoundingMode.UP).doubleValue(), 1, 3); table.setValueAt("" + new BigDecimal(Math.abs(Y2 - Y1)).setScale(3, RoundingMode.UP).doubleValue(), 2, 4); table.setValueAt("" + new BigDecimal(Math.abs(Y2 - Y1)).setScale(3, RoundingMode.UP).doubleValue(), 1, 4); } }); add(slider_2); slider_3 = new JSlider(); slider_3.setBounds(5, 55, 250, 20); slider_3.setBackground(color); slider_3.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { try { int value = slider_3.getValue(); ValueAxis domainAxis = plot.getDomainAxis(); Range range = domainAxis.getRange(); X2 = domainAxis.getLowerBound() + (value / 100.0) * range.getLength(); plot.removeDomainMarker(distanceTiers1); distanceTiers1 = new ValueMarker(X2); distanceTiers1.setPaint(Color.BLACK); plot.addDomainMarker(distanceTiers1); int i = 0; for (; i < graph.mas_x.size(); i++) { if (graph.mas_x.get(i).doubleValue() <= X2) { } else { break; } } Y2 = graph.mas_y.get(i).doubleValue(); } catch (Exception e1) { if (e.toString() == "java.lang.IndexOutOfBoundsException") Y1 = graph.mas_y.get(graph.mas_y.size() - 1).doubleValue(); } table.setValueAt("" + new BigDecimal(X1).setScale(3, RoundingMode.UP).doubleValue(), 1, 1); table.setValueAt("" + new BigDecimal(Y1).setScale(3, RoundingMode.UP).doubleValue(), 1, 2); table.setValueAt("" + new BigDecimal(X2).setScale(3, RoundingMode.UP).doubleValue(), 2, 1); table.setValueAt("" + new BigDecimal(Y2).setScale(3, RoundingMode.UP).doubleValue(), 2, 2); table.setValueAt("" + new BigDecimal(Math.abs(X2 - X1)).setScale(3, RoundingMode.UP).doubleValue(), 2, 3); table.setValueAt("" + new BigDecimal(Math.abs(X2 - X1)).setScale(3, RoundingMode.UP).doubleValue(), 1, 3); table.setValueAt("" + new BigDecimal(Math.abs(Y2 - Y1)).setScale(3, RoundingMode.UP).doubleValue(), 2, 4); table.setValueAt("" + new BigDecimal(Math.abs(Y2 - Y1)).setScale(3, RoundingMode.UP).doubleValue(), 1, 4); } }); add(slider_3); }
From source file:org.kievguide.controller.PlaceController.java
@RequestMapping(value = "/place", method = RequestMethod.GET) public ModelAndView placeById(@CookieValue(value = "userstatus", defaultValue = "guest") String useremail, @RequestParam("placeid") int placeid) { ModelAndView modelAndView = new ModelAndView(); Place place = placeService.searchPlaceById(placeid); place.setRatingvalue(new BigDecimal(place.getRatingvalue()).setScale(1, RoundingMode.UP).doubleValue()); String userStatus = Util.userPanel(useremail); String voteStatus;//from w ww.jav a2 s . c om User currentuser = userService.searchUser(useremail); Vote newvote = voteService.findByAuthorIdAndPlaceId(currentuser.getId().intValue(), placeid); if (newvote != null) { voteStatus = Util.userVote(newvote.getValue()); } else { voteStatus = Util.guestVote(place.getId()); } User author = userService.searchUserById(place.getAuthorid().getId()); String authorname = author.getFirstname() + " " + author.getLastname(); List<Comment> commentlist = commentService.findByPlaceid(place); for (Comment c : commentlist) { if (c.getAuthorid().getId() == currentuser.getId()) { c.setIsdelete("<a href=\"deletevote?commentid=" + c.getId() + "&placeid=" + c.getPlaceid().getId() + "\" class=\"comment-delete\"> </a>"); } } List<Place> anotherPlaces = anotherPlaces(place.getMetro().getId(), place.getId()); modelAndView.addObject("userstatus", userStatus); modelAndView.addObject("author", authorname); modelAndView.addObject("currentuser", currentuser); modelAndView.addObject("votestatus", voteStatus); modelAndView.addObject("place", place); modelAndView.addObject("anotherplaces", anotherPlaces); modelAndView.addObject("comments", commentlist); modelAndView.setViewName("place"); return modelAndView; }
From source file:my.mavenproject10.FileuploadController.java
@RequestMapping(method = RequestMethod.POST) ModelAndView upload(HttpServletRequest request, HttpServletResponse response) { boolean isMultipart = ServletFileUpload.isMultipartContent(request); String fileName = ""; int size = 0; ArrayList<String> result = new ArrayList<String>(); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try {/*from w w w . j av a 2 s.com*/ List items = upload.parseRequest(request); Iterator iterator = items.iterator(); while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); fileName = item.getName(); System.out.println("file name " + item.getName()); JAXBContext jc = JAXBContext.newInstance(CustomersType.class); SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); InputSource inputSource = new InputSource( new InputStreamReader(item.getInputStream(), "UTF-8")); SAXSource source = new SAXSource(xmlReader, inputSource); Unmarshaller unmarshaller = jc.createUnmarshaller(); CustomersType data2 = (CustomersType) unmarshaller.unmarshal(source); //System.out.println("size " + data2.getCustomer().size()); size = data2.getCustomer().size(); for (CustomerType customer : data2.getCustomer()) { System.out.println(customer.toString()); } // double summ = 0.0; HashMap<Integer, Float> ordersMap = new HashMap<Integer, Float>(); for (CustomerType customer : data2.getCustomer()) { for (OrderType orderType : customer.getOrders().getOrder()) { Float summPerOrder = 0.0f; //System.out.println(orderType); for (PositionType positionType : orderType.getPositions().getPosition()) { //System.out.println(positionType); summPerOrder += positionType.getCount() * positionType.getPrice(); summ += positionType.getCount() * positionType.getPrice(); } ordersMap.put(orderType.getId(), summPerOrder); } } summ = new BigDecimal(summ).setScale(2, RoundingMode.UP).doubleValue(); System.out.println(" " + summ); result.add(" " + summ); // HashMap<Integer, Float> customersMap = new HashMap<Integer, Float>(); for (CustomerType customer : data2.getCustomer()) { Float summPerCust = 0.0f; customersMap.put(customer.getId(), summPerCust); for (OrderType orderType : customer.getOrders().getOrder()) { for (PositionType positionType : orderType.getPositions().getPosition()) { summPerCust += positionType.getCount() * positionType.getPrice(); } } //System.out.println(customer.getId() + " orders " + summPerCust); customersMap.put(customer.getId(), summPerCust); } TreeMap sortedMap = sortByValue(customersMap); System.out.println(" " + sortedMap.keySet().toArray()[0] + " : " + sortedMap.get(sortedMap.firstKey())); result.add(" " + sortedMap.keySet().toArray()[0] + " : " + sortedMap.get(sortedMap.firstKey())); // TreeMap sortedMapOrders = sortByValue(ordersMap); System.out.println(" " + sortedMapOrders.keySet().toArray()[0] + " : " + sortedMapOrders.get(sortedMapOrders.firstKey())); result.add(" " + sortedMapOrders.keySet().toArray()[0] + " : " + sortedMapOrders.get(sortedMapOrders.firstKey())); // System.out.println(" " + sortedMapOrders.keySet().toArray()[sortedMapOrders.keySet().toArray().length - 1] + " : " + sortedMapOrders.get(sortedMapOrders.lastKey())); result.add(" " + sortedMapOrders.keySet().toArray()[sortedMapOrders.keySet().toArray().length - 1] + " : " + sortedMapOrders.get(sortedMapOrders.lastKey())); // System.out.println(" " + sortedMapOrders.size()); result.add(" " + sortedMapOrders.size()); // ArrayList<Float> floats = new ArrayList<Float>(sortedMapOrders.values()); Float summAvg = 0.0f; Float avg = 0.0f; for (Float f : floats) { summAvg += f; } avg = new BigDecimal(summAvg / floats.size()).setScale(2, RoundingMode.UP).floatValue(); System.out.println(" " + avg); result.add(" " + avg); } } catch (FileUploadException e) { System.out.println("FileUploadException:- " + e.getMessage()); } catch (JAXBException ex) { //Logger.getLogger(FileuploadController.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedEncodingException ex) { Logger.getLogger(FileuploadController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(FileuploadController.class.getName()).log(Level.SEVERE, null, ex); } catch (ParserConfigurationException ex) { Logger.getLogger(FileuploadController.class.getName()).log(Level.SEVERE, null, ex); } catch (SAXException ex) { Logger.getLogger(FileuploadController.class.getName()).log(Level.SEVERE, null, ex); } } ModelAndView modelAndView = new ModelAndView("fileuploadsuccess"); modelAndView.addObject("files", result); modelAndView.addObject("name", fileName); modelAndView.addObject("size", size); return modelAndView; }
From source file:com.ehi.carshare.Main.java
private void run(String logformat, String inputFile, String outputFile) throws Exception { printAllPossibles(logformat);/*ww w . jav a2 s. c o m*/ Parser<ApacheHttpLog> parser = new ApacheHttpdLoglineParser<ApacheHttpLog>(ApacheHttpLog.class, logformat); parser.ignoreMissingDissectors(); // Load file in memory File file = new File(inputFile); if (!file.exists()) { throw new RuntimeException("Input file does not exist"); } BufferedReader reader = new BufferedReader(new FileReader(file)); List<String> readLines = new ArrayList<String>(); String line = reader.readLine(); while (line != null) { readLines.add(line); line = reader.readLine(); } reader.close(); // Parse apache logs List<ApacheHttpLog> myRecords = new ArrayList<ApacheHttpLog>(); for (String readLine : readLines) { try { ApacheHttpLog myRecord = new ApacheHttpLog(); parser.parse(myRecord, readLine); if (myRecord.getAction() != null && "200".equals(myRecord.getStatus()) && myRecord.getPath() != null) { myRecords.add(myRecord); } } catch (Exception e) { /// e.printStackTrace(); } } // Group by action Map<String, List<ApacheHttpLog>> map = new HashMap<String, List<ApacheHttpLog>>(); for (ApacheHttpLog item : myRecords) { String key = item.getAction(); if (map.get(key) == null) { map.put(key, new ArrayList<ApacheHttpLog>()); } map.get(key).add(item); } // Collect stats List<ApacheHttpLogStats> recordStats = new ArrayList<ApacheHttpLogStats>(); for (Entry<String, List<ApacheHttpLog>> entry : map.entrySet()) { ApacheHttpLogStats stats = new ApacheHttpLogStats(); stats.setActionName(entry.getKey()); long responseCount = entry.getValue().size(); stats.setResponseCount(responseCount); long sum = 0; for (ApacheHttpLog myRecord : entry.getValue()) { sum = sum + myRecord.getResponseTime(); } BigDecimal average = new BigDecimal(sum) .divide(new BigDecimal(responseCount * 1000000), 2, RoundingMode.HALF_UP) .setScale(2, RoundingMode.UP); stats.setAverageResponseTime(average.toPlainString()); recordStats.add(stats); } // Write lines to file PrintWriter f0 = new PrintWriter(new FileWriter(outputFile)); f0.print(ApacheHttpLogStats.headerString()); for (ApacheHttpLogStats myRecordStats : recordStats) { f0.print(myRecordStats.toString()); } f0.close(); }
From source file:uk.org.rbc1b.roms.scheduled.DailyVolunteerEmailScheduledService.java
/** * Helper method to find the maximum number of volunteers who * should receive the contact details email based on the six month * regularity of this service.//from w w w .jav a 2 s . c o m * * @return integer of max number of volunteers */ private Integer findMaxVolunteersForEmail() { VolunteerSearchCriteria searchCriteria = new VolunteerSearchCriteria(); searchCriteria.setMaxResults(null); int totalVolunteerCount = volunteerDao.findVolunteersCount(searchCriteria); final DateTime dateTime = new DateTime(); if (dateTime.year().isLeap()) { return new BigDecimal(totalVolunteerCount) .divide(new BigDecimal(NUMBER_OF_DAYS_IN_LEAP_HALF_YEAR), RoundingMode.UP).intValue(); } return new BigDecimal(totalVolunteerCount) .divide(new BigDecimal(NUMBER_OF_DAYS_IN_HALF_YEAR), RoundingMode.UP).intValue(); }