Formatting a string number - c#

I want to format a string number. For example:
double number="118176";
It should look like 1181.71 or 1181,71.
I couldn't find any format type. I tried some of format types as ToString("#,0") but it didn't work.
Thanks for any advice.

First of all: a double variable can not take a string. But that aside, something like this should help:
double number = 1181.76;
string output = String.Format("{0:d2}", number);
This takes the number and creates a string from it using the decimal number format with 2 decimal places.
What you didn't say is why you expect the integer 118176 to magically turn into a double with two decimals? The only way would be
double number = 118176;
string output = String.Format("{0:d2}", number / 100.0);
EDIT
Doing what you describe in your comment is a bit more complex:
string priceString = nodeFareList.SelectSingleNode("GenQuoteDetails/TotAmt").InnerText;
double priceDouble = Convert.ToDouble(priceString) / 100.0;
price.InnerHtml += String.Format("{0:c}", priceDouble);
This converts the number in priceDouble to a string with the value formatted like a currency. If you do not want the currency symbol, use the following:
price.InnerHtml += String.Format("{0:d2}", priceDouble);

See Fixed-point on this page (assuming .net): http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
You'll have to divide by 100 first though.

I think, what you are looking for is this:
String.Format("{0:0,0.0}", 12345.67);
String.Format("{0:0,0}", 12345.67);
See here for details.

Use can refer the below code in case of Java.
1) First define the Decimal Format instance.
DecimalFormat decimalFormat = new DecimalFormat("####.##");
2) Then pass the decimal value to the 'format' method
String dummyString = decimalFormat.format(pressureValue).toString()
where pressureValue - Decimal number
Hope this will be useful.

Related

C# Convert currency to string, remove decimal point but keep decimals, prepend 0s to have fixed width

How do I convert money amount like this
38.50
, to fixed width like this
000003850
I dont want to add commas and decimal places as some suggested answers here are explaining how to do. Neither do I want to remove decimals, I only want to remove decimal point.
You can use string.Format() with custom format specifiers. See details here.
double dollars = 38.50; // your value
int temp = (int)(dollars * 100); // multiplication to get an integer
string result = string.Format("{0:000000000}", temp);
// Output: 000003850
Figured it out thanks to #ReedCopsey at How to remove decimal point from a decimal number in c#?.
decimal amount = 38.50m;
int fixed_amount = (int)Math.Truncate(amount * 100);
Console.WriteLine(fixed_amount.ToString("D9"));
OUTPUT:
000003850

How to always show 3 numbers after decimal point C#

I am currently displaying a number that is being rounded to 3 decimal places e.g. 0.31, using Math.Pow, the only problem is I want to display this number to say 0.310 (for styling purposes) does anyone know if this is possible?
The Fixed-Point Format Specifier can be used in a call to ToString or in string.Format:
double x = 1493.1987;
string s1 = x.ToString("F3");
string s2 = string.Format("Your total is {0:F3}, have a nice day.", x);
// s1 is "1493.199"
// s2 is "Your total is 1493.199, have a nice day."
Note that the Fixed-Point Format Specifier will always show the number of decimal digits you specify. For example:
double y = 1493;
string s3 = y.ToString("F3");
// s3 is "1493.000"
Use the format in the toString
double pi = 3.1415927;
string output = pi.ToString("#.000");
Here is an updated example that also works w/o having to call .ToString():
float a = 12.3578f;
double b = 12.3578d;
Console.WriteLine("The tolerance specs are: {0:F4} and: {1:F3}", a,b);
ANSWER: The tolerance specs are: 12.3578 and: 12.358

How to convert string to decimal with 3 decimal places?

string num = 23.6;
I want to know how can I convert it into decimal with 3 decimal places
like
decimal nn = 23.600
Is there any method?
I try my best..
First of all your string num = 23.6; won't even compile. You need to use double quotes with your strings like string num = "23.6";
If you wanna get this as a decimal, you need to parse it first with a IFormatProvider that have . as a NumberDecimalSeparator like InvariantCulture(if your CurrentCulture uses . already, you don't have to pass second paramter);
decimal nn = decimal.Parse(num, CultureInfo.InvariantCulture);
Now we have a 23.6 as a decimal value. But as a value, 23.6, 23.60, 23.600 and 23.60000000000 are totally same, right? No matter which one you parse it to decimal, you will get the same value as a 23.6M in debugger. Looks like these are not true. See Jon Skeet comments on this answer and his "Keeping zeroes" section on Decimal floating point in .NET article.
Now what? Yes, we need to get it's textual representation as 23.600. Since we need only decimal separator in a textual representation, The "F" Format Specifier will fits out needs.
string str = nn.ToString("F3", CultureInfo.InvariantCulture); // 23.600
There are two different concepts here.
Value
View
you can have a value of 1 and view it like 1.0 or 1.0000 or +000001.00.
you have string 23.6. you can convert it to decimal using var d = decimal.Parse("23.6")
now you have a value equals to 23.6 you can view it like 23.600 by using d.ToString("F3")
you can read more about formatting decimal values
the thing that works for me in my case is num.ToString("#######.###")
A decimal is not a string, it does not display the trailing zeros. If you want a string that displays your 3 decimal places including trailing zeros, you can use string.Format:
decimal nn= 23.5;
var formattedNumber = string.Format("{0,000}", nn);

Convert double to string, keep format - C#

Hi all. I have a double number (Ex: 0.000006). I want convert it to string type. But result is "6E-06". I dont want it, i want 0.000006".Thanks you so much
double a = 0.000006;
string resultString = a.toString();
I don't know many number after "." character
It's simple that if you want to show a number as exactly as what it looks, we can cast it to decimal and use the default ToString() like this:
var s = ((decimal)yourNumber).ToString();
//if yourNumber = 0.00000000000000000000000006
//just append the M after it:
var s = (0.00000000000000000000000006M).ToString();
Please check this article and find the format which suits your needs: http://www.csharp-examples.net/string-format-double/
Looks like this one is good enough:
String.Format("{0:0.00}", 123.4567);
Use String.Format() with the format specifier.
double a = 0.000006;
string formatted = String.Format("{0:F6}", a);
Try with the Roundtrip 'R' format specifier:
double a = 0.000006;
string resultString = a.ToString("R");
Double Rate_USD = Convert.ToDouble(txtRateUsd.Text);
string Rate_USD = txtRateUsd.Text;

Changing the number of integers on a output value after the decimal point

So I'm learning and practicing WP7 application development.
I'm working with integers (currency), and it seems to always display four integers after the decimal place. I'm trying to cut it down to just either ONE or TWO decimal places.
I've been trying to use the "my variable.ToString("C2")" (C for Currency, 2 for number of ints after the decimal)
I'm probably missing something obvious, but please help
decimal number = new decimal(1000.12345678);
string text = number.ToString("#.##");
Output:
1000,12
An other way:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencyDecimalDigits = 2;
decimal val = new decimal(1000.12345678);
string text = val.ToString("c", nfi);
When formatting a currency, NumberFormatInfo allows specifying following properties as well:
CurrencyDecimalDigits
CurrencyDecimalSeparator
CurrencyGroupSeparator
CurrencyGroupSizes
CurrencyNegativePattern
CurrencyPositivePattern
CurrencySymbol
See Custom Numeric Format Strings on MSDN for more examples
The "C" format string defines the currency specifier as described on MSDN. This will include the currency symbol for the current culture, or for a specific culture if supplied, e.g.
double amount = 1234.5678;
string formatted = amount.ToString("C", CultureInfo.CreateSpecificCulture("en-US"));
// This gives $1234.56
In your case, it seems that you have a limited set of currency symbols that you support, so I would suggest using the fixed point format specifier "F" instead. By default this will give you 2 decimal points, but you can specify a number to vary this, e.g.
double amount = 1234.5678;
string formatted = amount.ToString("F");
// This gives 1234.56
formatted = amount.ToString("F3");
// This gives 1234.567
Using the fixed point specifier will give you control over the number of decimal points and enable you to concatenate the currency symbol.
The only thing I would add to "sll" answer is to pay attention on Culture (they often forget to mantion this), like this (example)
string text = val.ToString("#.##", CultureInfo.InvariantCulture);
double total = 526.4134
string moneyValue = total.ToString("c");
This will display it in this format: $#.##

Categories

Resources