decimalformat

Remove trailing zeros from double

心已入冬 提交于 2019-12-01 01:09:35
问题 I would like to remove all trailing zeros without truncating or rounding the number if it doesn't have any. For example, the number could be something like 12.0 , in which case, the trailing zero should be removed. But the number could also be something almost irrational, like 12.9845927346958762... going on an on to the edge of the screen. Is there a way to setup DecimalFormat or some other class to cut of trailing zeros, while keeping the irrationality intact? 回答1: If you are willing to

Most elegant way to load csv with point as thousands separator in R

偶尔善良 提交于 2019-11-30 19:05:54
NB: To the best of my knowledge this question is not a duplicate! All the questios/answers I found are either how to eliminate points from data that are already in R or how to change the decimal point to a comma when loading it. I have a csv with numbers like: 4.123,98 . The problem is that because of the . the output becomes a character string matrix when loading with read.table , read.csv or read.csv2 . Changing dec to , doesn't help. My question What is the most elegant way to load this csv so that the numbers become e.g. 4123.98 as numeric? cryo111 Adapted from this post: Specify custom

Formating using DecimalFormat throws exception - “Cannot format given Object as a Number”

痴心易碎 提交于 2019-11-30 09:31:22
This might look like a repeated question but I tried in all the below links and can't get a proper answer. Cannot format given Object as a Number ComboBox Illegal Argument Exception But I'm not getting whats wrong. Here is my code DecimalFormat twoDForm = new DecimalFormat("#.##"); double externalmark = 1.86; double internalmark = 4.0; System.out.println(String.valueOf((externalmark*3+internalmark*1)/4)); String val = String.valueOf((externalmark*3+internalmark*1)/4); String wgpa1=twoDForm.format(val); // gives exception String wgpa2=twoDForm.format((externalmark*3+internalmark*1)/4)); //

Laravel validate decimal 0-99.99

a 夏天 提交于 2019-11-30 08:19:16
Laravel does not have a validation for decimal , so I need a regex or other validation method to validate a numeric value of 0 - 99.99 I have tried required|regex:^\d{0,2}(\.\d{1,2})?$/ required|regex:[1-9]\d?(\.\d\d?)?|0\.[1-9]\d?||0\.0[1-9] required|regex:/[1-9]\d?(\.\d\d?)?|0\.[1-9]\d?||0\.0[1-9]/ required|regex:/[\d]{2}.[\d]{2}/ required|regex:/[\d]{0-2}.[\d]{0,2}/ I am either getting "invalid format" when trying to enter 0.05 or unknown modifiers for preg_match any help with the regex needed to validate a decimal in laravel is appreciated (with optional values before or after decimal The

Java: Use DecimalFormat to format doubles and integers but keep integers without a decimal separator

十年热恋 提交于 2019-11-30 08:07:37
问题 I'm trying to format some numbers in a Java program. The numbers will be both doubles and integers. When handling doubles, I want to keep only two decimal points but when handling integers I want the program to keep them unaffected. In other words: Doubles - Input 14.0184849945 Doubles - Output 14.01 Integers - Input 13 Integers - Output 13 (not 13.00) Is there a way to implement this in the same DecimalFormat instance? My code is the following, so far: DecimalFormat df = new DecimalFormat("#

Most elegant way to load csv with point as thousands separator in R

风格不统一 提交于 2019-11-30 03:17:46
问题 NB: To the best of my knowledge this question is not a duplicate! All the questios/answers I found are either how to eliminate points from data that are already in R or how to change the decimal point to a comma when loading it. I have a csv with numbers like: 4.123,98 . The problem is that because of the . the output becomes a character string matrix when loading with read.table , read.csv or read.csv2 . Changing dec to , doesn't help. My question What is the most elegant way to load this

DecimalFormat的几种用法!

蓝咒 提交于 2019-11-30 02:59:36
package com.lee.test; import java.text.DecimalFormat; import java.util.Locale; /** * DecimalFormat的几种用法! * @author lee * @depict NumberFormat.getInstance()方法返回NumberFormat的一个实例( * 实际上是NumberFormat具体的一个子类,例如DecimalFormat), 这适合根 * 据本地设置格式化一个数字。你也可以使用非缺省的地区设置,例如德国。然后格式 * 化方法根据特定的地区规则格式化数字。这个程序也可以使用一个简单的形式: * NumberFormat.getInstance().format(1234.56) */ public class Demo_test { public static void main(String[] args) { // for (int i = 1; i <= 100; i++) { // String squence = new DecimalFormat("00000").format(i); // System.out.println(squence+","); // } // 得到本地的缺省格式 DecimalFormat df1 = new

How to use Java's DecimalFormat for “smart” currency formatting?

て烟熏妆下的殇ゞ 提交于 2019-11-29 16:03:00
问题 I'd like to use Java's DecimalFormat to format doubles like so: #1 - 100 -> $100 #2 - 100.5 -> $100.50 #3 - 100.41 -> $100.41 The best I can come up with so far is: new DecimalFormat("'$'0.##"); But this doesn't work for case #2, and instead outputs "$100.5" Edit: A lot of these answers are only considering cases #2 and #3 and not realizing that their solution will cause #1 to format 100 as "$100.00" instead of just "$100". 回答1: Does it have to use DecimalFormat ? If not, it looks like the

Java: Use DecimalFormat to format doubles and integers but keep integers without a decimal separator

你说的曾经没有我的故事 提交于 2019-11-29 05:46:54
I'm trying to format some numbers in a Java program. The numbers will be both doubles and integers. When handling doubles, I want to keep only two decimal points but when handling integers I want the program to keep them unaffected. In other words: Doubles - Input 14.0184849945 Doubles - Output 14.01 Integers - Input 13 Integers - Output 13 (not 13.00) Is there a way to implement this in the same DecimalFormat instance? My code is the following, so far: DecimalFormat df = new DecimalFormat("#,###,##0.00"); DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH);

DecimalFormat pattern

≡放荡痞女 提交于 2019-11-29 02:07:42
public static String formatAmountUpToTwoDecimalNumber(String amount) { if(amount==null || "".equals(amount)) { return ""; } Double doubleAmount = Double.valueOf(amount); double myAmount = doubleAmount.doubleValue(); NumberFormat f = new DecimalFormat("###,###,###,###,##0.00"); String s = f.format(myAmount); return s; } "###,###,###,###,##0.00" , What exactly is the purpose of this pattern ? I believe it serves two purposes to group numbers, that is put thousand seperator comma to append two zeros after decimal if decimal is missing that is convert 23 to 23.00 But why there is "0" instead of "#