Format number using decimal format in kotlin
This is the format you need: val dec = DecimalFormat(“#,###.##”) will print: 5.384,45 if you need always exactly 2 digits after the decimal point: val dec = DecimalFormat(“#,###.00”)
This is the format you need: val dec = DecimalFormat(“#,###.##”) will print: 5.384,45 if you need always exactly 2 digits after the decimal point: val dec = DecimalFormat(“#,###.00”)
The laravel(5?) between rule does always get it has to parse a decimal, you have to add numeric to it to validate correctly ‘required|numeric|between:0,99.99’,
You can specify locale for DecimalFormat this way: DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US); DecimalFormat format = new DecimalFormat(“##.########”, symbols);
Try this // Create a DecimalFormat that fits your requirements DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setGroupingSeparator(‘,’); symbols.setDecimalSeparator(‘.’); String pattern = “#,##0.0#”; DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols); decimalFormat.setParseBigDecimal(true); // parse the string BigDecimal bigDecimal = (BigDecimal) decimalFormat.parse(“10,692,467,440,017.120”); System.out.println(bigDecimal); If you are building an application with I18N support you should use DecimalFormatSymbols(Locale) Also keep in mind … Read more
If you want that for display purposes, use java.text.DecimalFormat: new DecimalFormat(“#.##”).format(dblVar); If you need it for calculations, use java.lang.Math: Math.floor(value * 100) / 100;
You can change the separator either by setting a locale or using the DecimalFormatSymbols. If you want the grouping separator to be a point, you can use an european locale: NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN); DecimalFormat df = (DecimalFormat)nf; Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers … Read more