## Examples of java.math.BigDecimal

• java.math.BigDecimal
ger=19, scale=2}
but
{@code 21/110 = 0.190 // integer=190, scale=3}

Note that for add, subtract, and multiply, the reduction in scale will equal the number of digit positions of the exact result which are discarded. If the rounding causes a carry propagation to create a new high-order digit position, an additional digit of the result is discarded than when no new digit position is created.

Other methods may have slightly different rounding semantics. For example, the result of the {@code pow} method using the{@linkplain #pow(int,MathContext) specified algorithm} canoccasionally differ from the rounded mathematical result by more than one unit in the last place, one {@linkplain #ulp() ulp}.

Two types of operations are provided for manipulating the scale of a {@code BigDecimal}: scaling/rounding operations and decimal point motion operations. Scaling/rounding operations ( {@link #setScale setScale} and {@link #round round}) return a {@code BigDecimal} whose value is approximately (or exactly) equalto that of the operand, but whose scale or precision is the specified value; that is, they increase or decrease the precision of the stored number with minimal effect on its value. Decimal point motion operations ( {@link #movePointLeft movePointLeft} and{@link #movePointRight movePointRight}) return a {@code BigDecimal} created from the operand by moving the decimalpoint a specified distance in the specified direction.

For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of {@code BigDecimal} methods. Thepseudo-code expression {@code (i + j)} is shorthand for "a{@code BigDecimal} whose value is that of the {@code BigDecimal}{@code i} added to that of the {@code BigDecimal}{@code j}." The pseudo-code expression {@code (i == j)} isshorthand for " {@code true} if and only if the{@code BigDecimal} {@code i} represents the same value as the{@code BigDecimal} {@code j}." Other pseudo-code expressions are interpreted similarly. Square brackets are used to represent the particular {@code BigInteger} and scale pair defining a{@code BigDecimal} value; for example [19, 2] is the{@code BigDecimal} numerically equal to 0.19 having a scale of 2.

Note: care should be exercised if {@code BigDecimal} objectsare used as keys in a {@link java.util.SortedMap SortedMap} orelements in a {@link java.util.SortedSet SortedSet} since{@code BigDecimal}'s natural ordering is inconsistent with equals. See {@link Comparable}, {@link java.util.SortedMap} or {@link java.util.SortedSet} for moreinformation.

All methods and constructors for this class throw {@code NullPointerException} when passed a {@code null} objectreference for any input parameter. @see BigInteger @see MathContext @see RoundingMode @see java.util.SortedMap @see java.util.SortedSet @author Josh Bloch @author Mike Cowlishaw @author Joseph D. Darcy

 `350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390` ```    priceSpinnerModel.addChangeListener(new ChangeListener() {         public void stateChanged(ChangeEvent ev) {           controller.removePropertyChangeListener(FurnitureController.Property.PRICE, priceChangeListener);           Object value = priceSpinnerModel.getValue();           controller.setPrice(value != null               ? new BigDecimal((Float)value).setScale(2, BigDecimal.ROUND_HALF_UP)               : (BigDecimal)value);           controller.addPropertyChangeListener(FurnitureController.Property.PRICE, priceChangeListener);         }       });     // Create VAT % label and its spinner bound to VALUE_ADDED_TAX_PERCENTAGE controller property     this.valueAddedTaxPercentageLabel = new JLabel(SwingTools.getLocalizedLabelText(preferences,         FurniturePanel.class, "valueAddedTaxPercentageLabel.text"));     final NullableSpinner.NullableSpinnerNumberModel valueAddedTaxPercentageSpinnerModel =         new NullableSpinner.NullableSpinnerNumberModel(0, 0, 100f, 0.1f);     this.valueAddedTaxPercentageSpinner = new AutoCommitSpinner(valueAddedTaxPercentageSpinnerModel);     valueAddedTaxPercentageSpinnerModel.setNullable(controller.getValueAddedTaxPercentage() == null);     valueAddedTaxPercentageSpinnerModel.setValue(controller.getValueAddedTaxPercentage() == null         ? null         : controller.getValueAddedTaxPercentage().floatValue() * 100);     final PropertyChangeListener valueAddedTaxPercentageChangeListener = new PropertyChangeListener() {       public void propertyChange(PropertyChangeEvent ev) {         valueAddedTaxPercentageSpinnerModel.setNullable(ev.getNewValue() == null);         valueAddedTaxPercentageSpinnerModel.setValue(ev.getNewValue() == null             ? null             : ((Float)ev.getNewValue()) * 100);       }     };     controller.addPropertyChangeListener(FurnitureController.Property.VALUE_ADDED_TAX_PERCENTAGE, valueAddedTaxPercentageChangeListener);     valueAddedTaxPercentageSpinnerModel.addChangeListener(new ChangeListener() {         public void stateChanged(ChangeEvent ev) {           controller.removePropertyChangeListener(FurnitureController.Property.VALUE_ADDED_TAX_PERCENTAGE, valueAddedTaxPercentageChangeListener);           Object value = valueAddedTaxPercentageSpinnerModel.getValue();           controller.setValueAddedTaxPercentage(value != null               ? new BigDecimal((Float)value / 100).setScale(3, BigDecimal.ROUND_HALF_UP)               : (BigDecimal)value);           controller.addPropertyChangeListener(FurnitureController.Property.VALUE_ADDED_TAX_PERCENTAGE, valueAddedTaxPercentageChangeListener);         }       });     ```
View Full Code Here

 `10751076107710781079108010811082108310841085108610871088` ```      return new DefaultTableCellRenderer() {           @Override           public Component getTableCellRendererComponent(JTable table,                Object value, boolean isSelected, boolean hasFocus,                int row, int column) {             BigDecimal valueAddedTaxPercentage = ((CatalogPieceOfFurniture)value).getValueAddedTaxPercentage();             if (valueAddedTaxPercentage != null) {               NumberFormat percentInstance = DecimalFormat.getPercentInstance();               percentInstance.setMinimumFractionDigits(valueAddedTaxPercentage.scale() - 2);               value = percentInstance.format(valueAddedTaxPercentage);             } else {               value = "";             }             setHorizontalAlignment(JLabel.RIGHT); ```
View Full Code Here

 `434435436437438439440441442443444445446447448449450451452453454455456457458` ```          }         }       }       setCreator(creator);       BigDecimal price = firstPiece.getPrice();       if (price != null) {         for (int i = 1; i < this.modifiedFurniture.size(); i++) {           if (!price.equals(this.modifiedFurniture.get(i).getPrice())) {             price = null;             break;           }         }       }       setPrice(price);       BigDecimal valueAddedTaxPercentage = firstPiece.getValueAddedTaxPercentage();       if (valueAddedTaxPercentage != null) {         for (int i = 1; i < this.modifiedFurniture.size(); i++) {           if (!valueAddedTaxPercentage.equals(this.modifiedFurniture.get(i).getValueAddedTaxPercentage())) {             valueAddedTaxPercentage = null;             break;           }         }       } ```
View Full Code Here

 `849850851852853854855856857858` ```  /**    * Sets the edited price.    */   public void setPrice(BigDecimal price) {     if (price != this.price) {       BigDecimal oldPrice = this.price;       this.price = price;       this.propertyChangeSupport.firePropertyChange(Property.ICON.name(), oldPrice, price);     }   } ```
View Full Code Here

 `867868869870871872873874875876` ```  /**    * Sets the edited value added tax percentage.    */   public void setValueAddedTaxPercentage(BigDecimal valueAddedTaxPercentage) {     if (valueAddedTaxPercentage != this.valueAddedTaxPercentage) {       BigDecimal oldValueAddedTaxPercentage = this.valueAddedTaxPercentage;       this.valueAddedTaxPercentage = valueAddedTaxPercentage;       this.propertyChangeSupport.firePropertyChange(Property.VALUE_ADDED_TAX_PERCENTAGE.name(), oldValueAddedTaxPercentage, valueAddedTaxPercentage);     }   } ```
View Full Code Here

 `901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958` ```      Boolean resizable = getResizable();       Boolean deformable = getDeformable();       Boolean doorOrWindow = getDoorOrWindow();       float [][] modelRotation = getModelRotation();       String creator = getCreator();       BigDecimal price = getPrice();       BigDecimal valueAddedTaxPercentage = getValueAddedTaxPercentage();       boolean defaultFurnitureLanguage = FurnitureLibrary.DEFAULT_LANGUAGE.equals(this.furnitureLanguageController.getFurnitureLangauge());             // Apply modification       int piecesCount = this.modifiedFurniture.size();       for (CatalogPieceOfFurniture piece : this.modifiedFurniture) {         int index = this.furnitureLibrary.getPieceOfFurnitureIndex(piece);         // Retrieve localized data         Map localizedNames = new HashMap();         Map localizedDescriptions = new HashMap();         Map localizedCategories = new HashMap();         for (String language : this.furnitureLibrary.getSupportedLanguages()) {           Object pieceName = this.furnitureLibrary.getPieceOfFurnitureLocalizedData(               piece, language, FurnitureLibrary.FURNITURE_NAME_PROPERTY);           if (pieceName != null) {             localizedNames.put(language, pieceName);           }           Object pieceDescription = this.furnitureLibrary.getPieceOfFurnitureLocalizedData(               piece, language, FurnitureLibrary.FURNITURE_DESCRIPTION_PROPERTY);           if (pieceDescription != null) {             localizedDescriptions.put(language, pieceDescription);           }           Object categoryName = this.furnitureLibrary.getPieceOfFurnitureLocalizedData(               piece, language, FurnitureLibrary.FURNITURE_CATEGORY_PROPERTY);           if (categoryName != null) {             localizedCategories.put(language, categoryName);           }         }         this.furnitureLibrary.deletePieceOfFurniture(piece);                 String pieceId = id != null || piecesCount == 1 ? id : piece.getId();         String pieceName = name != null && defaultFurnitureLanguage ? name : piece.getName();         String pieceDescription = description != null && defaultFurnitureLanguage ? description : piece.getDescription();         FurnitureCategory pieceCategory = category != null && defaultFurnitureLanguage ? category : piece.getCategory();         Content pieceModel = model != null ? model : piece.getModel();         Content pieceIcon = icon != null ? icon : piece.getIcon();         float pieceWidth = width != null ? width : piece.getWidth();         float pieceDepth = depth != null ? depth : piece.getDepth();         float pieceHeight = height != null ? height : piece.getHeight();         float pieceElevation = elevation != null ? elevation : piece.getElevation();         boolean pieceMovable = movable != null ? movable : piece.isMovable();         float [][] pieceModelRotation = modelRotation != null ? modelRotation : piece.getModelRotation();         String pieceCreator = creator != null || piecesCount == 1 ? creator : piece.getCreator();         boolean pieceResizable = resizable != null ? resizable : piece.isResizable();         boolean pieceDeformable = deformable != null ? deformable : piece.isDeformable();         BigDecimal piecePrice = price != null ? price : piece.getPrice();         BigDecimal pieceValueAddedTaxPercentage = valueAddedTaxPercentage != null             ? valueAddedTaxPercentage : piece.getValueAddedTaxPercentage();                 if (piece instanceof CatalogDoorOrWindow) {           CatalogDoorOrWindow opening = (CatalogDoorOrWindow)piece;           piece = new CatalogDoorOrWindow(pieceId, pieceName, pieceDescription, ```
View Full Code Here

 `505506507508509510511512513514515516517518519520521522523` ```                    case Types.DOUBLE:                         return new Double(resultSet.getDouble(index));                     case Types.DECIMAL:                     case Types.NUMERIC:                         BigDecimal num = resultSet.getBigDecimal(index);                         if (num == null) {                             break;                         }                         if (num.scale() > 0) {                             return new Double(num.doubleValue());                         } else {                             return new Long(num.longValue());                         }                     case Types.VARBINARY:                     case Types.BINARY:                         return resultSet.getString(index); ```
View Full Code Here

 `154155156157158159160161162163164165166167168169170171172173174175176177` ```      // This array list will produce the problem       // Remove it and the test case will succeed       ArrayList genericList = new ArrayList();       genericList.add("something");       entry.getMap().put("D", genericList);       entry.getMap().put("E", new BigDecimal("0.00"));     }     DozerExampleEntry mapped = dozerBeanMapper.map(entry, DozerExampleEntry.class);     // All the fields which are visited/mapped before the     // ArrayList are mapped successfully and to correct type     assertEquals("foobar", mapped.getMap().get("A"));     assertEquals(new Date(0), mapped.getMap().get("B"));     assertEquals(Boolean.TRUE, mapped.getMap().get("C"));     ArrayList expectedList = new ArrayList();     expectedList.add("something");     assertEquals(expectedList, mapped.getMap().get("D"));     assertNotSame(expectedList, mapped.getMap().get("D"));     // The BigDecimal was visited _after_ the ArrayList     // and thus converted to String due to the bug.     assertEquals(new BigDecimal("0.00"), mapped.getMap().get("E"));   } ```
View Full Code Here

 `317318319320321322323324325326327` ```     int[] intArray = { 1, 1 };     Integer[] integerArray = { new Integer(1), new Integer(1) };     custom.setAnArray(intArray);     custom.setArrayForLists(integerArray);     custom.setBigDecimalToInt(new BigDecimal(1));     custom.setIntToBigDecimal(1);     Date date = new Date();     custom.setDate(date);     GregorianCalendar calendar = new GregorianCalendar();     calendar.setTime(date); ```
View Full Code Here

 `27282930313233343536373839404142` ```  public void shouldMapPrimitiveTypes() throws Exception {     Source source = new Source();     source.file = "a";     source.url = "http://a";     source.type = "java.lang.String";     source.bigDecimal = new BigDecimal("1");     source.myDouble = new Double("1");     Destination result = mapper.map(source, Destination.class);     assertThat(result.file, equalTo(new File("a")));     assertThat(result.url, equalTo(new URL("http://a")));     assertThat(result.type, sameInstance(String.class));     assertThat(result.bigDecimal, equalTo(new Double("1")));     assertThat(result.myDouble, equalTo(new BigDecimal("1.0")));   } ```
View Full Code Here