TextBlock string format with dot WPF [duplicate] - c#

This question already has answers here:
Custom numeric format string to always display the sign
(6 answers)
Closed 8 years ago.
I want to format any number (integer or real) to a string representation which always has a sign (positive or negative) and a decimal separator, but no trailing zeroes.
Some samples:
3.14 => +3.14
12.00 => +12.
-78.4 => -78.4
-3.00 => -3.
Is it possible with one of the default ToString() implementations, or do I need write this myself?

Try something like this:
double x = -12.43;
string xStr = x.ToString("+0.#####;-0.#####");
But this wouldn't help to display trailing decimal point. You can handle such situations using this method:
public static string MyToString(double x)
{
return x == Math.Floor(x)
? x.ToString("+0;-0;0") + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
: x.ToString("+0.####;-0.####");
}

You can try like this:
string myFormatedString = number.ToString("+#;-#");

The format string you want to use is
ToString("N", CultureInfo.InvariantCulture) // Displays -12,445.68
See here for additional options for format strings

Related

How to check values numbers after the decimal point [duplicate]

This question already has answers here:
Remove trailing zeros
(23 answers)
Closed 5 years ago.
I have tried looking for a similar question here but I can only find how to count the number of decimal places.
What I want to know is: how do I find the value of the decimal places that are >0?
For example, if we have:
decimal value = 1.920m;
decimal value2 = 1.900m;
How do I check if the values after the decimal point are >0?
I want to be able to check this and restrict the display accordingly so I can display something like this:
1.92
1.9
Essentially you want to display the value with the max number of decimal places available and remove the trailing zeros. This is the easiest way to do it:
Console.WriteLine(value.ToString("G29")); // Output 1.92
Alternate solution (which works for numbers smaller than 0.00001m unlike the above solution). Though this doesn't look as neat as the previous solution using G29, this works better since it also covers numbers smaller than 0.00001:
Console.WriteLine(value.ToString("0.#############################")); // Output 1.92
We are using G29 since 29 is the maximum available digits for a decimal. The G or General Format Specifier is used to define the maximum number of significant digits that can appear in the result string. Any trailing zeros are truncated using this format specifier. You can read more about it here.
Input: 1.900m
Output: 1.9
Input: 14.571428571428571428571428571M
Output: 14.571428571428571428571428571
Input: 0.00001000000m
Output: 1E-05 (Using first solution G29)
Output: 0.00001 (Using second solution)
If i understand you right you can do something like this:
double x = 1.92;
x-=(int)x;
while(x%1>0){
x*=10;
}
Console.WriteLine(x);
output:
92
now you can check what you want on this number
If you want to convert to decimal only use this
static decimal? RemoveTrailingZeros(this decimal? value)
{
if (value == null) return null;
var format = $"0.{string.Join(string.Empty, Enumerable.Repeat("#", 29))}";
var strvalue = value.Value.ToString(format);
return ConvertToDecimalCultureInvariant(strvalue);
}
static decimal? ConvertToDecimalCultureInvariant(this string value)
{
decimal decValue;
if (!decimal.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out decValue))
{
return null;
}
return decValue;
}
Since the precision of a decimal is 29 hence Enumerable.Repeat("#", 29).
And use it as
var result = RemoveTrailingZeros(29.0000m);

Format a number to always have a sign and decimal separator [duplicate]

This question already has answers here:
Custom numeric format string to always display the sign
(6 answers)
Closed 8 years ago.
I want to format any number (integer or real) to a string representation which always has a sign (positive or negative) and a decimal separator, but no trailing zeroes.
Some samples:
3.14 => +3.14
12.00 => +12.
-78.4 => -78.4
-3.00 => -3.
Is it possible with one of the default ToString() implementations, or do I need write this myself?
Try something like this:
double x = -12.43;
string xStr = x.ToString("+0.#####;-0.#####");
But this wouldn't help to display trailing decimal point. You can handle such situations using this method:
public static string MyToString(double x)
{
return x == Math.Floor(x)
? x.ToString("+0;-0;0") + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
: x.ToString("+0.####;-0.####");
}
You can try like this:
string myFormatedString = number.ToString("+#;-#");
The format string you want to use is
ToString("N", CultureInfo.InvariantCulture) // Displays -12,445.68
See here for additional options for format strings

Convert a very Small number String to Decimal [duplicate]

This question already has answers here:
Convert from scientific notation string to float in C#
(2 answers)
Closed 9 years ago.
I want to convert a very Small number to Decimal,
Lets Say
String secondsStr = 0;
Decimal secondsValue;
Boolean success = Decimal.TryParse(secondsStr, out secondsValue);
But the problem is I have the string representation of it, 3.24E-08
String secondsStr = 3.24E-08;
Decimal secondsValue;
Boolean success = Decimal.TryParse(secondsStr, out secondsValue);
It always return success as false.
How can I parse that to get 0.00000003244657 ?
You can use TryParse with the NumberStyles argument:
var ok = Decimal.TryParse(secondsStr, NumberStyles.Any, null, out secondsValue);
I have used NumberStyles.Any which works.
Indicates that all styles except AllowHexSpecifier are used. This is a
composite number style.
Update: if it works with NulberStyles.Float depends on the current culture. If it uses . as decimal separator it works. So you can also use CultureInfo.InvariantCulture as third argument:
var ok = Decimal.TryParse(secondsStr, NumberStyles.Float, CultureInfo.InvariantCulture, out secondsValue);
Try this
Boolean c= decimal.TryParse("3.24E-08", System.Globalization.NumberStyles.Float, null, out a);

Convert int (number) to string with leading zeros? (4 digits) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Number formatting: how to convert 1 to "01", 2 to "02", etc.?
How can I convert int to string using the following scheme?
1 converts to 0001
123 converts to 0123
Of course, the length of the string is dynamic. For this sample, it is:
int length = 4;
How can I convert like it?
Use String.PadLeft like this:
var result = input.ToString().PadLeft(length, '0');
Use the formatting options available to you, use the Decimal format string. It is far more flexible and requires little to no maintenance compared to direct string manipulation.
To get the string representation using at least 4 digits:
int length = 4;
int number = 50;
string asString = number.ToString("D" + length); //"0050"
Use the ToString() method - standard and custom numeric format strings. Have a look at the MSDN article How to: Pad a Number with Leading Zeros.
string text = no.ToString("0000");
val.ToString("".PadLeft(length, '0'))

How to round and format a decimal correctly? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
I'm trying to get my decimals to display with four decimal places. The DB rounds my number to 4 decimal places, but it returns the number with trailing 0s (due to the decimal precision of the field), so something like 9.45670000. Then, when I do this:
string.Format("{0:#,#.####}", decimalValue);
The output I get on the page is 9.4567, which is what I want.
However, if the number returned from DB is 9.45600000, the output after doing the format is 9.456
But what I need to display is 9.4560
How do I format my decimal, so that the number of decimal places is always four?
UPDATE: Also, is it possible to use a variable (instead of .0000) if I wanted the number of decimal places to be determined dynamically?
string.Format("{0:N4}",decimalValue);
Standard Numeric Format Strings
Custom Numeric Format Strings
To set the precision dynamically you can do the following:
double value = 9.4560000;
int precision = 4;
string format = String.Format("{{0:N{0}}}",precision);
string valuestring = String.Format(format, value);
string.Format({0:#,#0.0000}, decimalValue);
Use String.Format -
decimal d =123.47
string specifier="{0:0,0.0000}"; // You need to get specifier dynamically here..
String.Format(specifier, d); // "123.4700"
Try this:
string.Format("{0:#,###.0000}", 9.45600000);
Adding the zeroes in the format forces a zero to be output if there is not a digit to put there.
To add the zeroes with the number of zeroes driven programmatically you could do this:
int x = 5;
string fmt = "{0:#,###." + new string('0', x) + "}";
string.Format(fmt, 9.456000000);

Categories

Resources