What Is Type of 123_456_789

What is type of 123_456_789?

From the documentation
(The Swift Programming Language -> Language Guide -> The Basics
-> Numeric Literals):

Numeric literals can contain extra formatting to make them easier to
read. Both integers and floats can be padded with extra zeros and can
contain underscores to help with readability. Neither type of
formatting affects the underlying value of the literal:

let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1

So your 123_456_789 is a integer literal, and identical to 123456789.
You can insert the underscores wherever you want, not only as a
"thousands separator", such as 1_2_3_4_5_6_7_8_9 or 1_23_4567_89, if you like to write obfuscated code.

When i use auto bi = 123456789, in C++, is it always assigned as an int?

Some options:

auto bi = "123456789";          // const char*
auto bi2 = 12345; // int
auto bi3 = 123456789; // int (when int is 32 bits or more )
auto bi4a = 123456789L; // long
auto bi4b = 178923456789L; // long long! (L suffix asked for long, but got long long so that the number can fit)
auto bi5a = 123456789LL; // long long
auto bi5b = 123456784732899; // long long (on my system it is long long, but might be different on ILP64; there is would just be an int)
auto bi6 = 123456789UL; // unsigned long
auto bi7 = 123456789ULL; // unsigned long long

All of the examples above depend on the system that you use.

In the standard, in [lex.icon] Table 5 — Types of integer literals is referenced:

The type of an integer literal is the first of the corresponding list
in Table 5 in which its value can be represented.

If we look at the table for decimal literals we see that even the effects of the U and L suffixes depend upon what size can be accommodated:

Sample Image

input double number of 123456789 in test giving output of 1.23456789E8 using java.util.Scanner

While I would not recommend using the double primitive type to represent money (BigDecimal, int, or long would be more appropriate), you can use System.out.printf() to avoid seeing the exponential form:

System.out.printf("The amount you entered in sales for the year is: $%.2f", number);

Looping numbers in python; 123456789

Try printing i in your loop and see what happens.

Numbers with hyphen separator (123456789 to 123-45-6789)

Use basic number-formatting:

yourNumber.ToString("###-##-####");

Update:

Ok, if you already have it as a string, you can split it and add hyphens:

var result = str.Substring(0,3) + "-" 
+ str.Substring(3,2) + "-"
+ str.Substring(5);

...or on secont thought, perhaps String.Insert() is simpler:

// insert "-" after position 3, then after position 6 in the returned string.
result = str.Insert(3, "-").Insert(6, "-");

How can i convert 123456789 to 1.234.567,89 on basis of **Currency Symbol** like Euro(€) etc?

Use .ToString("C"). Something like this:

var cost = 123456789;
Console.WriteLine(cost.ToString("C"));

That uses the current culture of the system it is running on. So if your regional settings are setup for Euro, it will show as Euro.

To force it to use a specific culture, you can use this (this example uses France, so it shows Euro):

cost.ToString("C", CultureInfo.CreateSpecificCulture("fr-FR"))

More info is here, including how to change the default culture (instead of specifying it each time): https://docs.microsoft.com/en-us/globalization/locale/currency-formatting-in-the-dotnet-framework



Related Topics



Leave a reply



Submit