Converting Integer to String with comma for thousands
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));
Output: 35,634,646
How to format a number with commas as thousands separators?
I used the idea from Kerry's answer, but simplified it since I was just looking for something simple for my specific purpose. Here is what I have:
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
}
function test(x, expect) {
const result = numberWithCommas(x);
const pass = result === expect;
console.log(`${pass ? "✓" : "ERROR ====>"} ${x} => ${result}`);
return pass;
}
let failures = 0;
failures += !test(0, "0");
failures += !test(100, "100");
failures += !test(1000, "1,000");
failures += !test(10000, "10,000");
failures += !test(100000, "100,000");
failures += !test(1000000, "1,000,000");
failures += !test(10000000, "10,000,000");
if (failures) {
console.log(`${failures} test(s) failed`);
} else {
console.log("All tests passed");
}
.as-console-wrapper {
max-height: 100% !important;
}
.NET String.Format() to add commas in thousands place for a number
String.Format("{0:n}", 1234); // Output: 1,234.00
String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876
Converting a full column of integer into string with thousands separated using comma in pandas
Using apply
to format the numbers.
In [40]: ps.apply('{:,}'.format)
Out[40]:
CountryA 12,345,678
CountryB 3,242,342
dtype: object
In [41]: ps
Out[41]:
CountryA 12345678
CountryB 3242342
dtype: int64
How can I parse a string with a comma thousand separator to a number?
Yes remove the commas:
let output = parseFloat("2,299.00".replace(/,/g, ''));
console.log(output);
How to convert an integer to a comma separated string
Use format
:
>>> bankBalance = 1000000
>>> format(bankBalance, ",")
'1,000,000'
>>>
How can I format a String number to have commas and round?
You might want to look at the DecimalFormat
class; it supports different locales (eg: in some countries that would get formatted as 1.000.500.000,57
instead).
You also need to convert that string into a number, this can be done with:
double amount = Double.parseDouble(number);
Code sample:
String number = "1000500000.574";
double amount = Double.parseDouble(number);
DecimalFormat formatter = new DecimalFormat("#,###.00");
System.out.println(formatter.format(amount));
How to print a number using commas as thousands separators
Locale unaware
'{:,}'.format(value) # For Python ≥2.7
f'{value:,}' # For Python ≥3.6
Locale aware
import locale
locale.setlocale(locale.LC_ALL, '') # Use '' for auto, or force e.g. to 'en_US.UTF-8'
'{:n}'.format(value) # For Python ≥2.7
f'{value:n}' # For Python ≥3.6
Reference
Per Format Specification Mini-Language,
The
','
option signals the use of a comma for a thousands separator. For a locale aware separator, use the'n'
integer presentation type instead.
Related Topics
How to Pass Arithmetic Operators to a Method in Java
Noclassdeffounderror in Java: Com/Google/Common/Base/Function
Java Inetaddress.Getlocalhost(); Returns 127.0.0.1 ... How to Get Real Ip
How to Access Parameters in a Restful Post Method
How to Configure Encoding in Maven
How Is Countdownlatch Used in Java Multithreading
Is String Literal Pool a Collection of References to the String Object, or a Collection of Objects
What Is the Convention for Word Separator in Java Package Names
Java.Util.Zip.Zipexception: Error in Opening Zip File
The Meaning of Noinitialcontextexception Error
Why Is T Bounded by Object in the Collections.Max() Signature
How to Reference Another Property in Java.Util.Properties
Synchronizing on an Integer Value
Java Regular Expression to Extract Content Within Square Brackets
How to Sort an Arraylist Using Multiple Sorting Criteria
Eclipse: Attach Source/Javadoc to a Library via a Local Property