number-formatting

Problem parsing currency text to decimal type

核能气质少年 提交于 2019-11-26 04:26:54
问题 I am trying to parse a string like \"$45.59\" into a decimal. For some reason I am getting exception that the input was not in the correct format. I don\'t care about all the localization stuff because this is not going to be a global program. Here is what I am doing. Do you see any problems? NumberFormatInfo MyNFI = new NumberFormatInfo(); MyNFI.NegativeSign = \"-\"; MyNFI.NumberDecimalSeparator = \".\"; MyNFI.NumberGroupSeparator = \",\"; MyNFI.CurrencySymbol = \"$\"; decimal d = decimal

How to print formatted BigDecimal values?

南笙酒味 提交于 2019-11-26 03:55:16
问题 I have a BigDecimal field amount which represents money, and I need to print its value in the browser in a format like $123.00 , $15.50 , $0.33 . How can I do that? (The only simple solution which I see myself is getting floatValue from BigDecimal and then using NumberFormat to make two-digit precision for the fraction part). 回答1: public static String currencyFormat(BigDecimal n) { return NumberFormat.getCurrencyInstance().format(n); } It will use your JVM’s current default Locale to choose

Phone number formatting on iOS

天大地大妈咪最大 提交于 2019-11-26 03:47:57
问题 I have a text field where the user enters data. It\'s a phone number field. If the user enters 1234567890 , I want it displayed as (123)-(456)-7890 as the user types. How is this possible? 回答1: This will help you Format (xxx) xxx-xxxx - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { int length = (int)[self getLength:textField.text]; //NSLog(@"Length = %d ",length); if(length == 10) { if(range.length == 0) return NO;

How can I format a String number to have commas and round?

只愿长相守 提交于 2019-11-26 03:46:25
What is the best way to format the following number that is given to me as a String? String number = "1000500000.574" //assume my value will always be a String I want this to be a String with the value: 1,000,500,000.57 How can I format it as such? NullUserException You might want to look at the DecimalFormat class; it supports different locales (eg: in some countries that would get formatted as 1.000.500.000,57 instead). You also need to convert that string into a number, this can be done with: double amount = Double.parseDouble(number); Code sample: String number = "1000500000.574"; double

PHP How do I round down to two decimal places?

假如想象 提交于 2019-11-26 03:40:47
问题 I need to round down a decimal in PHP to two decimal places so that: 49.955 becomes... 49.95 I have tried number_format , but this just rounds the value to 49.96 . I cannot use substr because the number may be smaller (such as 7.950). I\'ve been unable to find an answer to this so far. Any help much appreciated. 回答1: This can work: floor($number * 100) / 100 回答2: Here is a nice function that does the trick without using string functions: <?php function floorp($val, $precision) { $mult = pow

Formatting numbers (decimal places, thousands separators, etc) with CSS

僤鯓⒐⒋嵵緔 提交于 2019-11-26 03:38:54
问题 Is it possible to format numbers with CSS? That is: decimal places, decimal separator, thousands separator, etc. 回答1: You can use Number.prototype.toLocaleString() . It can also format for other number formats, e.g. latin, arabic, etc. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString 回答2: The CSS working group has publish a Draft on Content Formatting in 2008. But nothing new right now. 回答3: Well, for any numbers in Javascript I use next

Parse a Number from Exponential Notation

我的梦境 提交于 2019-11-26 03:30:46
问题 I need to parse the string \"1.2345E-02\" (a number expressed in exponential notation) to a decimal data type, but Decimal.Parse(\"1.2345E-02\") simply throws an error 回答1: It is a floating point number, you have to tell it that: decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float); 回答2: It works if you specify NumberStyles.Float: decimal x = decimal.Parse("1.2345E-02", NumberStyles.Float); Console.WriteLine(x); // Prints 0.012345 I'm not entirely sure why this isn

Java Currency Number format

这一生的挚爱 提交于 2019-11-26 02:09:00
问题 Is there a way to format a decimal as following: 100 -> \"100\" 100.1 -> \"100.10\" If it is a round number, omit the decimal part. Otherwise format with two decimal places. 回答1: I doubt it. The problem is that 100 is never 100 if it's a float, it's normally 99.9999999999 or 100.0000001 or something like that. If you do want to format it that way, you have to define an epsilon, that is, a maximum distance from an integer number, and use integer formatting if the difference is smaller, and a

PowerShell, formatting values in another culture

旧街凉风 提交于 2019-11-26 01:47:48
问题 Is there an easy way in PowerShell to format numbers and the like in another locale? I\'m currently writing a few functions to ease SVG generation for me and SVG uses . as a decimal separator, while PowerShell honors my locale settings ( de-DE ) when converting floating-point numbers to strings. Is there an easy way to set another locale for a function or so without sticking .ToString((New-Object Globalization.CultureInfo \"\")) after every double variable? Note: This is about the locale used

Excel CSV - Number cell format

倖福魔咒の 提交于 2019-11-26 01:45:27
问题 I produce a report as an CSV file. When I try to open the file in Excel, it makes an assumption about the data type based on the contents of the cell, and reformats it accordingly. For example, if the CSV file contains ...,005,... Then Excel shows it as 5. Is there a way to override this and display 005? I would prefer to do something to the file itself, so that the user could just double-click on the CSV file to open it. I use Excel 2003. 回答1: There isn’t an easy way to control the