decimalformat

How to set Locale on GSON (for decimal number separators)

最后都变了- 提交于 2019-12-25 05:13:31
问题 I know you can set date formatting, but how do you set other locale specific stuff like decimal number formatting? (I mean comma vs. dot) 回答1: Short answer: you don't. You seem to be somewhat confusing the textual representation of a number and an actual numeric value. While JSON is a text-based data interchange format, a number (vs. a string) in JSON is a numeric value and is not affected by locale. Section 2.4 of the JSON specification provides the specific definition (emphasis mine): 2.4.

PHP format string to numbers seperated by two decimal points

僤鯓⒐⒋嵵緔 提交于 2019-12-25 05:05:56
问题 User can input values in following format: 1.00 1 1,00 Now I need to return this values formated by using two decimal points (,). When I use: number_format($request->amount, 2, ',','') And use "1,00" as input I get this error: Notice: A non well formed numeric value encountered What's the best way to solve this and still be able to handle all three types of input? Here's a short example: input of user:|output: 1.00 1,00 1 1,00 1,51 1,51 回答1: The following should work: $request->amount = str

Java DecimalFormat creates error when enforcing exponent sign

空扰寡人 提交于 2019-12-24 03:50:54
问题 In Java, I am trying to get DecimalFormat to enforce the sign on an exponent sign. When it is positive I need a plus sign to appear. From what I have read this seems like a no brainer, but for myself it always throws up an error. I appreciate that there may be other methods to achieve my goal, but I would like to understand why in this specific method the error is occurring. Double result = 123.456; String sresult; //This works NumberFormat formatter = new DecimalFormat("0.00000E00"); sresult

different locale pattern on standalone Java application and Web application

半城伤御伤魂 提交于 2019-12-24 02:09:34
问题 Why there is a different outputs for below code when tried as a standalone Java application and as a Web application (Servlet)? Code: Locale locale=new java.util.Locale("nb");// locale for Norwegian, Bokmal (Norway) DecimalFormat decimalFormatter=(DecimalFormat) DecimalFormat.getInstance(locale); System.out.println(decimalFormatter.toLocalizedPattern()); System.out.println(locale.getDisplayLanguage()); System.out.println(decimalFormatter.format(1234.567)); Outputs: standalone JAVA application

Format, 2 decimal places for double and 0 for integer in java

雨燕双飞 提交于 2019-12-20 02:13:13
问题 I am trying to format a double to exact 2 decimal places if it has fraction, and cut it off otherwise using DecimalFormat So, I'd like to achieve next results: 100.123 -> 100.12 100.12 -> 100.12 100.1 -> 100.10 100 -> 100 Variant #1 DecimalFormat("#,##0.00") 100.1 -> 100.10 but 100 -> 100.00 Variant #2 DecimalFormat("#,##0.##") 100 -> 100 but 100.1 -> 100.1 Have any ideas what pattern to choose in my case? 回答1: The only solution i reached is to use if statement like was mentioned here: https:

Rounding with DecimalFormat in Java

老子叫甜甜 提交于 2019-12-19 05:22:30
问题 Let's look at the following statements in Java. System.out.println(new DecimalFormat("0").format(2.4)); //returns 2 System.out.println(new DecimalFormat("0").format(2.5)); //returns 2 <---Concentrate here System.out.println(Math.round(2.5)); //returns 3 System.out.println(new DecimalFormat("0").format(2.6)); //returns 3 System.out.println(new DecimalFormat("0").format(3.5)); //returns 4 In the above statements, all other cases are obvious except the following. System.out.println(new

How to deserialize a float value with a localized decimal separator with Jackson

爷,独闯天下 提交于 2019-12-19 03:12:58
问题 The input stream I am parsing with Jackson contains latitude and longitude values such as here: { "name": "product 23", "latitude": "52,48264", "longitude": "13,31822" } For some reason the server uses commas as the decimal separator which produces an InvalidFormatException . Since I cannot change the server output format I would like to teach Jackson's ObjectMapper to handle those cases. Here is the relevant code: public static Object getProducts(final String inputStream) { ObjectMapper

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

我与影子孤独终老i 提交于 2019-12-18 13:16:42
问题 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 what's 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

Laravel validate decimal 0-99.99

让人想犯罪 __ 提交于 2019-12-18 12:49:39
问题 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