Questions tagged [number-formatting]

Number formatting is the process of converting a numeric value into a string suitable for display. Things to take into account are the decimal/thousands separator, digit grouping, the shape of the numbers, how a percentage or currency is displayed, and so on.

Number formatting is the process of converting a numeric value into a string suitable for display.

Questions about formatting numbers in different formats for

  • decimal/thousands separator
  • digit grouping
  • the shape of the numbers
  • percentage
  • currency formatting

This should be mostly used with tag in which language it should be done.

2429 questions
1643
votes
29 answers

How to convert decimal to hexadecimal in JavaScript

How do you convert decimal values to their hexadecimal equivalent in JavaScript?
Luke Smith
  • 20,674
  • 7
  • 27
  • 28
916
votes
31 answers

Format number to always show 2 decimal places

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:…
Varada
  • 12,891
  • 13
  • 45
  • 67
900
votes
29 answers

How to print number with commas as thousands separators?

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…
Elias Zamaria
  • 80,938
  • 29
  • 103
  • 136
637
votes
24 answers

Show a number to two decimal places

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()…
Rich Bradshaw
  • 67,265
  • 44
  • 170
  • 236
611
votes
6 answers

How to output numbers with leading zeros in JavaScript?

Is there a way to prepend leading zeros to numbers so that it results in a string of fixed length? For example, 5 becomes "05" if I specify 2 places.
chris
  • 6,329
  • 2
  • 16
  • 14
471
votes
13 answers

C# convert int to string with padding zeros?

In C# I have an integer value which need to be convereted to string but it needs to add zeros before: For Example: int i = 1; When I convert it to string it needs to become 0001 I need to know the syntax in C#.
Pinu
  • 6,620
  • 13
  • 50
  • 76
400
votes
8 answers

How to add leading zeros?

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 …
baz
  • 5,909
  • 11
  • 31
  • 37
384
votes
8 answers

How to format a floating number to fixed width in Python

How do I format a floating number to a fixed width with the following requirements: Leading zero if n < 1 Add trailing decimal zero(s) to fill up fixed width Truncate decimal digits past fixed width Align all decimal points For example: %…
hobbes3
  • 22,472
  • 23
  • 78
  • 114
347
votes
35 answers

How to format numbers by prepending 0 to single-digit numbers?

I want to format a number to have two digits. The problem is caused when 0–9 is passed, so I need it to be formatted to 00–09. Is there a number formatter in JavaScript?
Keith Power
  • 12,337
  • 19
  • 60
  • 118
328
votes
17 answers

Using String Format to show decimal up to 2 places or simple integer

I have got a price field to display which sometimes can be either 100 or 100.99 or 100.9, What I want is to display the price in 2 decimal places only if the decimals are entered for that price , for instance if its 100 so it should only show 100…
Mr A
  • 5,778
  • 23
  • 76
  • 133
254
votes
4 answers

Force R not to use exponential notation (e.g. e+10)?

Can I force R to use regular numbers instead of using the e+10-like notation? I have: 1.810032e+09 # and 4 within the same vector and want to see: 1810032000 # and 4 I am creating output for an old fashioned program and I have to write a text…
Matt Bannert
  • 25,237
  • 34
  • 134
  • 195
235
votes
6 answers

How to format a number 0..9 to display with 2 digits (it's NOT a date)

I'd like to always show a number under 100 with 2 digits (example: 03, 05, 15...) How can I append the 0 without using a conditional to check if it's under 10? I need to append the result to another String, so I cannot use printf.
realtebo
  • 19,593
  • 31
  • 85
  • 151
218
votes
10 answers

java : convert float to String and String to float

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…
lola
  • 5,209
  • 10
  • 44
  • 59
195
votes
7 answers

Format / Suppress Scientific Notation from Python Pandas Aggregation Results

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.…
191
votes
12 answers

Display a decimal in scientific notation

How can I display this: Decimal('40800000000.00000000000000') as '4.08E+10'? I've tried this: >>> '%E' % Decimal('40800000000.00000000000000') '4.080000E+10' But it has those extra 0's.
Greg
  • 39,830
  • 86
  • 217
  • 286
1
2 3
99 100