How to display a number with always 2 decimal points using BigDecimal?

BigDecimal is immutable, any operation on it including setScale(2, BigDecimal.ROUND_HALF_UP) produces a new BigDecimal. Correct code should be BigDecimal bd = new BigDecimal(1); bd.setScale(2, BigDecimal.ROUND_HALF_UP); // this does change bd bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println(bd); output 1.00 Note – Since Java 9 BigDecimal.ROUND_HALF_UP has been deprecated and you should now use RoundingMode.ROUND_HALF_UP.

How can I limit the number of decimal points in a UITextField?

Implement the shouldChangeCharactersInRange method like this: // Only allow one decimal point // Example assumes ARC – Implement proper memory management if not using. – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; NSArray *arrayOfString = [newString componentsSeparatedByString:@”.”]; if ([arrayOfString count] > 2 ) return NO; return YES; } This creates … Read more

What is the decimal separator symbol in JavaScript?

According to the specification, a DecimalLiteral is defined as: DecimalLiteral :: DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt . DecimalDigits ExponentPartopt DecimalIntegerLiteral ExponentPartopt and for satisfying the parseFloat argument: Let inputString be ToString(string). Let trimmedString be a substring of inputString consisting of the leftmost character that is not a StrWhiteSpaceChar and all characters to the right of that … Read more

In jQuery, what’s the best way of formatting a number to 2 decimal places?

If you’re doing this to several fields, or doing it quite often, then perhaps a plugin is the answer. Here’s the beginnings of a jQuery plugin that formats the value of a field to two decimal places. It is triggered by the onchange event of the field. You may want something different. <script type=”text/javascript”> // … Read more