How to separate thousands

How to separate thousands

How to separate thousands with space [duplicate]

I would like to format number so that the every thousand should be separated with a space.

I feel very bad to have written this makeshift function but as I haven’t mastered regular expressions, it’s the only way I found to do it. And of course it won’t work if the number is converted in a scientific format.

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

3 Answers 3

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

This seems like a much better fit for the format() function rather than bothering with regular expressions. The format() function exists to format numbers

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

I agree with the other answers that using other tools (such as format ) is the best approach. But if you really want to use a regular expression and substitution, then here is an approach that works using Perl’s look ahead.

Basically it looks for a digit that is followed by 1 or more sets of 3 digits (followed by a non-digit or the end of string) and replaces the digit with itself plus a space (the look ahead part does not appear in the replacement because it is not part of the match, more a condition on the match).

Not the answer you’re looking for? Browse other questions tagged regex r or ask your own question.

Linked

Related

Hot Network Questions

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

How to Use Comma as Thousand Separator in JavaScript

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

Artturi Jalli

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

To comma-separate thousands in a big number in JavaScript, use the built-in toLocaleString() method.

It localizes the number to follow a country-specific number formatting. To separate thousands with commas, localize the number to the USA. To do this, pass the ‘en-US’ locale code as an argument to the toLocaleString() method.

Alternative Approaches

The toLocaleString method works fine. It is still worthwhile to notice there are 2 other ways you to comma-separate thousands in a number in JavaScript.

Let’s briefly introduce these approaches.

Intl.NumberFormatter

In JavaScript, there is an internationalization namespace called Intl. This provides you with ways to be language-sensitive in:

You can thus use this namespace to format numbers for different localizations. To do this, use the Intl.NumberFormat() object by providing it the localization code. In case you want to use commas as thousand separators, use the en-US.

Custom Thousand-Separating Function

It is recommended to use a built-in function to separate thousands with commas.

But you can also write a custom implementation to for example practice your RegEx skills.

To implement your own thousand-comma-separator function, you need to:

Here is how it looks in code:

RegEx Part Explained in Detail

First things first, if you are wondering what regex is, it means Regular Expression.

It is a pattern-matching technique used broadly across developers when looking for matching words.

It is like a CTRL-F on steroids.

Not only can you match with exact words, but also words with a specific format, such as email addresses or phone numbers.

To learn more about regex in general, check this guide.

For someone familiar with regex, in this example, the regular expression thousands = /\B(?=(\d<3>)+(?!\d))/g:

So \B(?=(\d<3>)+(?!\d)) checks that one or more groups of exactly three digits follows, but ensures no additional digits follow that.

For instance, if you use this regular expression on 987654321.00:

Every point left to 3 is matched by the positive lookahead as 321 is 3 digits in length. The negative lookahead then checks that the multiple of 3 digits does not have any digits after it.

The result will be 987,654,321.00

Conclusion

Today you learned how to separate thousands with commas in a big number in JavaScript.

How would I separate thousands with space in C#

Assume I have the following decimal number that I have to format so that every thousand should be separated with a space:

I have tried Decimal.ToString(«0 000.00»). Although this works pretty well when the number is 1897.11. But when it’s 897.11 I get 0 897.11.

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

6 Answers 6

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

Pass in a custom NumberFormatInfo with a custom NumberGroupSeparator property, and use the #,# format to tell it to do number groups. This example uses the invariant culture’s number format as its basis.

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

You need a custom number format provider where you change the character yourself:

Here I also specify to format it as a number (using «n» ), please see this link for other formatting options:

In this case, it won’t work if you format as a currency or a percentage, because as you may already note the NumberFormatInfo has separate properties for those.

You will received : 134 566 548,00

Don’t forget your culture.

with a custom format string, but use ‘#’ not ‘0’s a 0 is a mandatory token it will be there no matter how long the number is, the pound sign (#) is optional token, it will only be used for digits actually in the number.

NOTE. I’ll leave this here, but @Tim S’ answer is a better one.

When I try this I see it only generates the first separator. I guess the separator only works for commas and periods here. As a workaround (up to the maximum size number you expect), try this instead.

Sweden Use thousand separator as Space(» «) and Decimal point separator as Comma(,). We can achieve this in the following way.

Number formatting

When dealing with numeric values, there are several major items to pay attention to:

Are the values currency (monetary) values?

Currencies require separate treatment from other numeric values. See Currency formatting for details.

The character used as the thousands separator

In the United States, this character is a comma (,). In Germany, it is a period (.). Thus one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space.

The character used as the decimal separator

In the United States, this character is a period (.). In Germany, it is a comma (,). Thus one thousand twenty-five and seven tenths is displayed as 1,025.7 in the United States and 1.025,7 in Germany.

The way negative numbers are displayed

The negative sign can be used at the beginning of the number, but it can also be used at the end of the number. Alternatively, the number can be displayed with parentheses around it or even in a color such as red. Thus a negative five hundred and twenty-seven could be displayed as: В

The shape of the numbers and representation of the number 10

Numbers might, of course, be shaped differently from one locale to another. Also, numbers in some locales might not correspond directly to there digits (0-9) used in, say, the United States. (See Table 1 below)

Table 1: Digits used for a variety of scripts.

Note how some scripts have one more digit than Latin or Arabic. This ‘extra digit’ represents the number 10.

Korean regularly uses both a Sino-Korean system and a native Korean system. Everything that can be counted will use one of the two systems, but seldom both.

Digit grouping

This refers to the number of digits contained between each separator for all digit groups that appear to the left of the decimal separator. For example, the 3-digit group is used for most cultures, such as for English (United States): 123,456,789.00. However, notice that Hindi uses a 2-digit grouping, except for the 3-digit grouping for denoting hundreds: 12,34,56,789.00

The placement of the percent sign (%)

It can be written several ways: 98%, 98 %, 98 pct, %98. Thus you should never assume that you can hard-code the percent sign.

The user can define preferred number-formatting parameters by making selections from the Numbers tab of the Customize Regional Options property sheet, within the Regional And Language Options property sheet. (See Figure 1 below.)

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

Figure 1: Selecting the preferred number formatting

In addition to in-line numbers, there may be additional systems used for numbered lists. Examples include Roman numerals for many locales and Iroha for Japan.

Decimal Separators: Points or Commas?

I’m sure you’re already familiar with decimal numbers, but have you ever wondered: “What’s the best symbol to separate them from integers? In today’s post we’re going to talk about this tiny symbol that we use so much, but which also changes depending on the country you might find it in! The International System of Units recognises two decimal separators: the point and the comma. They must be written on the line, on the same line as the digit, like this:

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

The use of decimal separators

As you know, decimal separators are symbols used to mark the difference between the whole and the decimal parts of a number. Although an attempt was made to unify the use of a single symbol, both are now accepted, and this can cause confusion.

Therefore, we’re going to look at which symbols are used depending on which part of the world you’re in:

How to separate thousands. Смотреть фото How to separate thousands. Смотреть картинку How to separate thousands. Картинка про How to separate thousands. Фото How to separate thousands

Europe

The majority of European countries use the decimal comma. Among them are Spain, France, Norway, the Czech Republic, Denmark, and more. However, it’s important to note that the United Kingdom is an exception because they tend to follow the Imperial System, which uses the decimal point. Curiously, Switzerland and Liechtenstein are different, as they use a point as a decimal separator, and an apostrophe (‘) for thousands.

America

The countries found to the north, like the U.S.A and Canada, use the decimal point, although the comma is used in the Francophone area of Canada as well. Countries closer to Central America, such as Mexico and the Caribbean Islands, also use the decimal point. However, South American countries such as Venezuela, Chile, Argentina, Colombia, and Uruguay, among others, use the comma.

Africa

There is no official standard used across Africa, but we know that some countries like Zimbabwe, South Africa, and Cameroon use the decimal comma. Others such as Botswana, Kenya, and Nigeria, use decimal points.

Oceania

Australia, New Zealand, as well as the islands and archipelagos close to them, use the same system as the rest of the Anglophone countries. Therefore, the decimal separator they use is a point.

Different countries in Asia use different decimal separators, sometimes within the same country! The decimal point is generally used in countries such as China, Japan, Malaysia, Singapore, Sri Lanka, The Philippines, etc. Some others use the decimal comma, as is the case in Indonesia and Mongolia.

Separating thousands

To separate thousands, the International Bureau of Weights and Measures declared in 2003 that the proper way to write numbers with more than four figures is by separating them into groups of three and leaving a space between each group. When it comes to a large figure with decimals, we write the decimal separator in its corresponding place and then we separate the figures into groups of three (leaving the space between each group of three), counting to the right or to the left from the symbol. This fantastic blog gives a great summary along with examples.

Despite this, it’s still common in countries that use decimal commas to find thousands separated by a dot. And in countries where a point is used as a decimal separator, a comma is usually used to separate thousands.

So, for example, twelve thousand five hundred with a decimal of five zero is written differently depending on the country:

Keep in mind that there are many countries, and each one has a different monetary and measurement system. Therefore, their decimal separators also change. Register with Smartick and keep learning with us!

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *