number-formatting

How to convert decimal to hexadecimal in JavaScript

落花浮王杯 提交于 2019-11-25 22:23:41
问题 How do you convert decimal values to their hexadecimal equivalent in JavaScript? 回答1: Convert a number to a hexadecimal string with: hexString = yourNumber.toString(16); And reverse the process with: yourNumber = parseInt(hexString, 16); 回答2: If you need to handle things like bit fields or 32-bit colors, then you need to deal with signed numbers. The JavaScript function toString(16) will return a negative hexadecimal number which is usually not what you want. This function does some crazy

How to print number with commas as thousands separators?

巧了我就是萌 提交于 2019-11-25 21:58:22
问题 I am trying to print an integer in Python 2.6.1 with commas as thousands separators. For example, I want to show the number 1234567 as 1,234,567 . How would I go about doing this? I have seen many examples on Google, but I am looking for the simplest practical way. It does not need to be locale-specific to decide between periods and commas. I would prefer something as simple as reasonably possible. 回答1: Locale unaware '{:,}'.format(value) # For Python ≥2.7 f'{value:,}' # For Python ≥3.7

Format number to always show 2 decimal places

时光总嘲笑我的痴心妄想 提交于 2019-11-25 21:49:48
问题 I would like to format my numbers to always display 2 decimal places, rounding where applicable. Examples: number display ------ ------- 1 1.00 1.341 1.34 1.345 1.35 I have been using this: parseFloat(num).toFixed(2); But it\'s displaying 1 as 1 , rather than 1.00 . 回答1: This works fine in FF4: parseFloat(Math.round(num3 * 100) / 100).toFixed(2); Live Demo var num1 = "1"; document.getElementById('num1').innerHTML = parseFloat(Math.round(num1 * 100) / 100).toFixed(2); var num2 = "1.341";

Show a number to two decimal places

廉价感情. 提交于 2019-11-25 21:48:31
问题 What\'s the correct way to round a PHP string to two decimal places? $number = \"520\"; // It\'s a string from a database $formatted_number = round_to_2dp($number); echo $formatted_number; The output should be 520.00 ; How should the round_to_2dp() function definition be? 回答1: You can use number_format(): return number_format((float)$number, 2, '.', ''); Example: $foo = "105"; echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00 This function returns a string . 回答2: Alternatively

How to add leading zeros?

天大地大妈咪最大 提交于 2019-11-25 21:43:31
问题 I have a set of data which looks something like this: anim <- c(25499,25500,25501,25502,25503,25504) sex <- c(1,2,2,1,2,1) wt <- c(0.8,1.2,1.0,2.0,1.8,1.4) data <- data.frame(anim,sex,wt) data anim sex wt anim2 1 25499 1 0.8 2 2 25500 2 1.2 2 3 25501 2 1.0 2 4 25502 1 2.0 2 5 25503 2 1.8 2 6 25504 1 1.4 2 I would like a zero to be added before each animal id: data anim sex wt anim2 1 025499 1 0.8 2 2 025500 2 1.2 2 3 025501 2 1.0 2 4 025502 1 2.0 2 5 025503 2 1.8 2 6 025504 1 1.4 2 And for

java : convert float to String and String to float

社会主义新天地 提交于 2019-11-25 20:45:24
How could I convert from float to string or string to float? In my case I need to make the assertion between 2 values string (value that I have got from table) and float value that I have calculated. String valueFromTable = "25"; Float valueCalculated =25.0; I tried from float to string: String sSelectivityRate = String.valueOf(valueCalculated ); but the assertion fails Petar Ivanov Using Java’s Float class. float f = Float.parseFloat("25"); String s = Float.toString(25.0f); To compare it's always better to convert the string to float and compare as two floats. This is because for one float

Excel CSV - Number cell format

烈酒焚心 提交于 2019-11-25 19:24:20
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. There isn’t an easy way to control the formatting Excel applies when opening a .csv file. However listed below are three approaches that might help. My

Format / Suppress Scientific Notation from Python Pandas Aggregation Results

一笑奈何 提交于 2019-11-25 18:56:12
How can one modify the format for the output from a groupby operation in pandas that produces scientific notation for very large numbers? I know how to do string formatting in python but I'm at a loss when it comes to applying it here. df1.groupby('dept')['data1'].sum() dept value1 1.192433e+08 value2 1.293066e+08 value3 1.077142e+08 This suppresses the scientific notation if I convert to string but now I'm just wondering how to string format and add decimals. sum_sales_dept.astype(str) Granted, the answer I linked in the comments is not very helpful. You can specify your own string converter