Questions tagged [formatting]

The process of transforming text or data for presentation. A typical example would be the transformation of a decimal into a particular currency format with the correct number of decimal places.

Code Format/Mark-up

Also called programming style or coding convention, a set of guide-lines how to style and format your text to make it more read- and maintain-able.

Formatting data

Determines how you should print a date, a double or any other data-type, for example by adding indentation, white-spaces or punctuation, or by presenting different aspects of data, e.g. showing only day and month of a date.

12180 questions
2121
votes
51 answers

How to print a number with commas as thousands separators in JavaScript

I am trying to print an integer in JavaScript 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? Here is how I am doing it: function numberWithCommas(x) { x =…
Elias Zamaria
  • 80,938
  • 29
  • 103
  • 136
2076
votes
66 answers

How to format numbers as currency strings

I would like to format a price in JavaScript. I'd like a function which takes a float as an argument and returns a string formatted like this: "$ 2,500.00" What's the best way to do this?
Daniel Magliola
  • 27,613
  • 56
  • 154
  • 235
1342
votes
14 answers

How to prettyprint a JSON file?

I have a JSON file that is a mess that I want to prettyprint. What's the easiest way to do this in Python? I know PrettyPrint takes an "object", which I think can be a file, but I don't know how to pass a file in. Just using the filename doesn't…
Colleen
  • 18,089
  • 12
  • 42
  • 70
1108
votes
15 answers

How can I pad an integer with zeros on the left?

How do you left pad an int with zeros when converting to a String in java? I'm basically looking to pad out integers up to 9999 with leading zeros (e.g. 1 = 0001).
Omar Kooheji
  • 50,943
  • 65
  • 176
  • 234
1053
votes
10 answers

How to escape braces (curly brackets) in a format string in .NET

How can brackets be escaped in using string.Format. For example: String val = "1,2,3" String.Format(" foo {{0}}", val); This example doesn't throw an exception, but outputs the string foo {0}. Is there a way to escape the brackets?
Pop Catalin
  • 57,418
  • 22
  • 86
  • 111
913
votes
21 answers

How do I change Eclipse to use spaces instead of tabs?

By default Eclipse indents with a hard tab character. How do I change it to spaces?
Brian Deacon
  • 19,494
  • 12
  • 36
  • 40
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
612
votes
28 answers

How can I convert byte size into a human-readable format in Java?

How can I convert byte size into a human-readable format in Java? Like 1024 should become "1 Kb" and 1024*1024 should become "1 Mb". I am kind of sick of writing this utility method for each project. Is there a static method in Apache Commons for…
Igor Mukhin
  • 13,058
  • 17
  • 48
  • 60
605
votes
45 answers

Format JavaScript date as yyyy-mm-dd

I have a date with the format Sun May 11,2014. How can I convert it to 2014-05-11 using JavaScript? function taskDate(dateMilli) { var d = (new Date(dateMilli) + '').split(' '); d[2] = d[2] + ','; return [d[0], d[1], d[2],…
user3625547
  • 6,079
  • 3
  • 10
  • 6
567
votes
28 answers

Commonly accepted best practices around code organization in JavaScript

As JavaScript frameworks like jQuery make client side web applications richer and more functional, I've started to notice one problem... How in the world do you keep this organized? Put all your handlers in one spot and write functions for all the…
hugoware
  • 33,265
  • 24
  • 58
  • 70
561
votes
10 answers

Eclipse: Set maximum line length for auto formatting?

I am working with Java. If I hit Ctrl+Shift+F in Eclipse Helios, it will auto format my code. At a certain point, it wraps lines. I would like to increase the maximum line length. How can I do this?
Nick Heiner
  • 108,809
  • 177
  • 454
  • 689
551
votes
14 answers

The shortest possible output from git log containing author and date

How can I show a git log output with (at least) this information: * author * commit date * change I want it compressed to one line per log entry. What's the shortest possible format for that? (tried --format=oneline but that does not show the…
Jesper Rønn-Jensen
  • 91,561
  • 40
  • 112
  • 147
535
votes
9 answers

Pad a number with leading zeros in JavaScript

In JavaScript, I need to have padding. For example, if I have the number 9, it will be "0009". If I have a number of say 10, it will be "0010". Notice how it will always contain four digits. One way to do this would be to subtract the number minus 4…
Nate Pet
  • 38,422
  • 114
  • 251
  • 393
514
votes
18 answers

Convert a date format in PHP

I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL); however I don't know how the date function requires a timestamp, and I can't get a timestamp from this string. How is this possible?
matthy
  • 7,468
  • 9
  • 35
  • 47
508
votes
40 answers

std::string formatting like sprintf

I have to format std::string with sprintf and send it into file stream. How can I do this?
Max Frai
  • 52,556
  • 73
  • 182
  • 293
1
2 3
99 100