decimal-point
Convert decimal mark when reading numbers as input
You may do it the locale-aware way: import locale # Set to users preferred locale: locale.setlocale(locale.LC_ALL, ”) # Or a specific locale: locale.setlocale(locale.LC_NUMERIC, “en_DK.UTF-8”) print locale.atof(“3,14”) Read this section before using this method.
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.
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
Simplest way of getting the number of decimals in a number in JavaScript [duplicate]
Number.prototype.countDecimals = function () { if(Math.floor(this.valueOf()) === this.valueOf()) return 0; return this.toString().split(“.”)[1].length || 0; } When bound to the prototype, this allows you to get the decimal count (countDecimals();) directly from a number variable. E.G. var x = 23.453453453; x.countDecimals(); // 9 It works by converting the number to a string, splitting at the . … Read more
Leave only two decimal places after the dot
string.Format is your friend. String.Format(“{0:0.00}”, 123.4567); // “123.46”
Javascript: formatting a rounded number to N decimals
I think that there is a more simple approach to all given here, and is the method Number.toFixed() already implemented in JavaScript. simply write: var myNumber = 2; myNumber.toFixed(2); //returns “2.00” myNumber.toFixed(1); //returns “2.0” etc…
Decimal separator comma (‘,’) with numberDecimal inputType in EditText
A workaround (until Google fix this bug) is to use an EditText with android:inputType=”numberDecimal” and android:digits=”0123456789.,”. Then add a TextChangedListener to the EditText with the following afterTextChanged: public void afterTextChanged(Editable s) { double doubleValue = 0; if (s != null) { try { doubleValue = Double.parseDouble(s.toString().replace(‘,’, ‘.’)); } catch (NumberFormatException e) { //Error } } … Read more