number-formatting

Is there a way to check if a string can be a float in C?

狂风中的少年 提交于 2019-12-18 05:39:05
问题 Checking if it can be an int is easy enough -- just check that every digit is between '0' and '9' . But a float is harder. I found this, but none of the answers really work. Consider this code snippet, based on the top (accepted) answer: float f; int ret = sscanf("5.23.fkdj", "%f", &f); printf("%d", ret); 1 will be printed. Another answer suggested using strpbrk , to check if certain illegal characters are present, but that wouldn't work either because 5fin7 wouldn't be legal, but inf would.

java.lang.NumberFormatException: For input string: “22”

▼魔方 西西 提交于 2019-12-17 20:39:20
问题 public void loadFromFile(String filename) { File file = new File(filename); BufferedReader br; try { br = new BufferedReader(new FileReader(file)); numberOfAttributes = Integer.parseInt(br.readLine()); } ... } Above is my program: I am trying to read from a txt file where the first line is the number 22 and nothing more. I don't know why the program gives me an exception. 回答1: Try stripping any whitespace from the string: numberOfAttributes = Integer.parseInt(br.readLine().trim()); 回答2: I

php converting formatted int to int

旧城冷巷雨未停 提交于 2019-12-17 20:21:52
问题 Sorry for asking this but, I cant found a solution that will get this -> 7,000 to 7000. I'm using intval() and number_format() but it will just give me 7 not 7000. $myVal = '7,000'; echo intval($myVal); this returns 7 same goes with number_format() what am I doing wrong? 回答1: intval() won't work with the string you have because of the comma. You can remove the comma by using str_replace() and then calling intval() like so: echo intval(str_replace(',', '', $myVal)) 回答2: If you are using PHP >=

Formatting Complex Numbers

ⅰ亾dé卋堺 提交于 2019-12-17 19:37:31
问题 For a project in one of my classes we have to output numbers up to five decimal places.It is possible that the output will be a complex number and I am unable to figure out how to output a complex number with five decimal places. For floats I know it is just: print "%0.5f"%variable_name Is there something similar for complex numbers? 回答1: For questions like this, the Python documentation should be your first stop. Specifically, have a look at the section on string formatting. It lists all the

Is there an R function to format number using unit prefix

坚强是说给别人听的谎言 提交于 2019-12-17 18:33:53
问题 Is there a R function (or any package) allowing to format numbers (integer) using standard unit prefix (Kilo, Mega etc ...), so 10 -> 10 100 -> 1K 0.01 - > 10m etc ... I can do it myself but I would prefer to not reinvent the wheel. 回答1: require(sitools) f2si(80000) [1] "80 k" f2si(8E12) [1] "8 T" It seems to be very simplistic as it appends two spaces if no SI prefix is used: f2si(80) [1] "80 " The function is easy to modify to include rounding. I also fixed the issue with appended spaces.

Format negative amount of USD with a minus sign, not brackets (Java)

僤鯓⒐⒋嵵緔 提交于 2019-12-17 11:22:12
问题 How do I get NumberFormat.getCurrencyInstance() to print negative USD currency values with a minus sign? 回答1: It requires a little tweaking of the DecimalFormat returned by NumberFormat.getCurrencyInstance() to do it in a locale-independent manner. Here's what I did (tested on Android): DecimalFormat formatter = (DecimalFormat)NumberFormat.getCurrencyInstance(); String symbol = formatter.getCurrency().getSymbol(); formatter.setNegativePrefix(symbol+"-"); // or "-"+symbol if that's what you

What is the default Precision and Scale for a Number in Oracle?

狂风中的少年 提交于 2019-12-17 10:47:40
问题 When creating a column of type NUMBER in Oracle, you have the option of not specifying a precision or scale. What do these default do if you don't specify them? 回答1: NUMBER (precision, scale) If a precision is not specified, the column stores values as given. If no scale is specified, the scale is zero. A lot more info at: http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT1832 回答2: The NUMBER type can be specified in different styles: Resulting Resulting

Is it possible in matlab to explicitly format the output numbers?

时光毁灭记忆、已成空白 提交于 2019-12-17 10:01:40
问题 I know about MATLAB's format long , format short , eng ... and so on. But short and long will always display a predefined number of decimals, with an exponent, and for example, format bank will display always 2 decimals. Is there an option to put your explicit format, in a "fortran way", like f8.3 --> 1234.678 ? I'm looking for a way to display numbers with 4 decimal points, and the rest ahead of the decimal point, with no exponent. 回答1: I don't know of a way to specify a global format of the

format a number with commas and decimals in C# (asp.net MVC3)

廉价感情. 提交于 2019-12-17 09:20:08
问题 I Need to display a number with commas and decimal point. Eg: Case 1 : Decimal number is 432324 (This does not have commas or decimal Points) Need to display it as 432,324.00 but not 432,324 Case 2 : Decimal number is 2222222.22 (This does not have commas) Need to display it as 2,222,222.22 I tried ToString("#,##0.##") . But It is Not Formatting it 回答1: int number = 1234567890; Convert.ToDecimal(number).ToString("#,##0.00"); You will get the result 1,234,567,890.00 . 回答2: Maybe you simply

as.numeric with comma decimal separators?

巧了我就是萌 提交于 2019-12-17 05:00:25
问题 I have a large vector of strings of the form: Input = c("1,223", "12,232", "23,0") etc. That's to say, decimals separated by commas, instead of periods. I want to convert this vector into a numeric vector. Unfortunately, as.numeric(Input) just outputs NA . My first instinct would be to go to strsplit , but it seems to me that this will likely be very slow. Does anyone have any idea of a faster option? There's an existing question that suggests read.csv2 , but the strings in question are not