Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decimal notation using G29

Tags:

c#

I am using G29 for removing two trailoring zeros.

 decimal d = 48.90000000M;                
 Console.WriteLine(d.ToString("G29"));

Output : 48.9

But my requirement is 48.90. How to achieve this ?

Thanks.

like image 471
Bokambo Avatar asked Feb 26 '13 11:02

Bokambo


People also ask

How to turn decimal into string?

To convert a Decimal value to its string representation using a specified culture and a specific format string, call the Decimal. ToString(String, IFormatProvider) method.

How to change decimal to string in c#?

You can use the decimal. ToString override to specify a formatting. decimal amount = 120.00m; string str = amount. ToString("0.00");

How to Convert decimal to string in asp net c#?

Method 1: string a = "100.00", b = "50.00"; string Total = (string)(Convert. ToDecimal(a) + Convert. ToDecimal(b));

What is ToString c?

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier. value.ToString("C3", CultureInfo.CurrentCulture)

What is decimal notation?

In Mathematics, the decimal notation is an alternate method to represent the fractional value. It means that it is the fractional representation using base 10. The fractions can be converted into decimals and vice versa.

What is the value of G in scientific notation?

Answer: = 3.456 × 10 11. (scientific notation) = 3.456e11. (scientific e notation) = 345.6 × 10 9. (engineering notation) (billion; prefix giga- (G)) = 345600000000.

How to convert a number to scientific notation?

How to Convert a Number to Scientific Notation. The proper format for scientific notation is a x 10^b where a is a number or decimal number such that the absolute value of a is greater than or equal to one and less than ten or, 1 ≤ | a | < 10. b is the power of 10 required so that the scientific notation is...

How to round to the power of 10 in scientific notation?

Multiply the decimal number by 10 raised to the power indicated. See the Scientific Notation Calculator to add, subtract, multiply and divide numbers in scientific notation or E notation. To round significant figures use the Significant Figures Calculator . To see how standard form is similar to scientific notation visit Standard Form Calculator .


Video Answer


2 Answers

I'd imagine you want F2 (if you always want two decimal places), not G29.

like image 193
Rawling Avatar answered Oct 08 '22 11:10

Rawling


G converts the number to its most compact form; Use F to have the trailing zero.

After all the most compact form of 48.9000000M is 48.9.

From Microsoft Docs:

The general ("G") format specifier converts a number to the most compact of either fixed-point or scientific notation, depending on the type of the number and whether a precision specifier is present. The precision specifier defines the maximum number of significant digits that can appear in the result string. If the precision specifier is omitted or zero, the type of the number determines the default precision, as indicated in the following table.
source

like image 33
Russ Clarke Avatar answered Oct 08 '22 11:10

Russ Clarke