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.

2824 questions
1892
votes
30 answers

How to convert decimal to hexadecimal in JavaScript

How do you convert decimal values to their hexadecimal equivalent in JavaScript?
Luke Smith
  • 23,504
  • 8
  • 29
  • 28
1204
votes
37 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
  • 16,026
  • 13
  • 48
  • 69
1181
votes
30 answers

How to print a number using commas as thousands separators

How do I print an integer with commas as thousands separators? 1234567 ⟶ 1,234,567 It does not need to be locale-specific to decide between periods and commas.
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
798
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
  • 8,219
  • 4
  • 18
  • 14
768
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
  • 71,795
  • 44
  • 182
  • 241
536
votes
15 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
  • 7,310
  • 16
  • 53
  • 77
484
votes
10 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
  • 28,078
  • 24
  • 87
  • 116
475
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
  • 6,817
  • 11
  • 36
  • 37
449
votes
36 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
  • 13,891
  • 22
  • 66
  • 135
391
votes
18 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
  • 6,448
  • 25
  • 83
  • 137
289
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
  • 27,631
  • 38
  • 141
  • 207
273
votes
8 answers

Format / Suppress Scientific Notation from 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.…
271
votes
7 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
  • 23,922
  • 37
  • 112
  • 189
247
votes
13 answers

Display a decimal in scientific notation

How can I display 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
  • 45,306
  • 89
  • 231
  • 297
235
votes
4 answers

Suppress Scientific Notation in Numpy When Creating Array From Nested List

I have a nested Python list that looks like the following: my_list = [[3.74, 5162, 13683628846.64, 12783387559.86, 1.81], [9.55, 116, 189688622.37, 260332262.0, 1.97], [2.2, 768, 6004865.13, 5759960.98, 1.21], [3.74, 4062, 3263822121.39,…
donopj2
  • 3,933
  • 5
  • 23
  • 30
1
2 3
99 100