Formatting values - c#

Hi i have a int example as 3 i need to format it as 003 . is the only way is convert to a string and concat and convert back ?

I guess this is what you want:
int n = 3;
string formatted = n.ToString("000");
Alternatively:
string formatted = String.Format("{0:000}", n);
More info here.

You can apply the .ToString("000"); method.

Debug.WriteLine(3.ToString("000"));
You can parse the resulting string value by using int.Parse or int.TryParse:
Debug.WriteLine(int.Parse("003"));
See Custom Numeric Format Strings

If it's an int object, the leading zeros will always be removed, regardless if you convert it to a string and back.

use the pad functionint i = 1;
i.ToString().PadLeft(3, '0');

Related

Is there a more optimal way to concatenate this string

I was wondering if there's a more simple way to concatenate this string instead of declaring a temporary variable
string tempValue = "000000000000000" + moneyValue;
moneyValue= tempValue.Substring((tempValue).Length - 15, 15);
I'm looking to a shorter way of achieving same result , is there any?
It looks like you are looking for a more succinct way to left-pad a string with zeroes.
.NET has a built-in method for that:
moneyValue = moneyValue.PadLeft(15, '0');
PadLeft works when you start with a string. If you want to combine zero padding with formatting of a number, you can use D15 format for integers
int moneyValue = 123456;
string moneyString = $"{moneyValue:D15}";
or a custom format for other numeric types
decimal moneyValue = 1234.56;
string moneyString = $"{moneyValue:000000000000000}";

c# remove trailing 0 for INR currency conversion?

I use following code to convert input to comma separated string in INR:
decimal input = 1111111111.59m;
string result = input.ToString("C", new CultureInfo("EN-in"));
I want to remove the trailing 0s now, how do i do this?
for example:
decimal input = 1111111111.00m;
Output should be 1111111111
string result = input.ToString("c0", new CultureInfo("EN-in"));
Update:
So you want output "123.45" for input 123.45 and output "123" for input 123.00.
You can't achieve these 2 different formats without conditional operator, String.Format() will produce only one output format for you.
The code is simple though:
string format = Decimal.Round(input) == input ? "c0" : "c";
string output = input.ToString(format);
string output = input.ToString("0");
Following code should work :
string results = input.ToString("0.##");
The simplest thing to convert is convert into int.
int d = convert.toInt32(1111.00);
or use any math function as suggested.
How to remove decimal part from a number in C#
How do I format a C# decimal to remove extra following 0's?
Edit
As I understand just try
Console.WriteLine(d.ToString("0.#####"));
Seet this url :- Best way to display decimal without trailing zeroes

How to make string of specified length from another string c#

For example I have strings like:
"5", "8", "14", "260"
and I want to get result like:
"ST00000005", "ST00000008", "ST00000014", "ST00000260"
result string length is 10 chars. How can I do it?
I would store it as int not as string. Then you can use ToString with the appropriate format specifier D8. That has f.e. the advantage that you can increase the number:
int number = 5;
string result = String.Format("ST{0}", number.ToString("D8"));
or without ToString but only String.Format:
string result = String.Format("ST{0:D8}", number);
Read: Standard Numeric Format Strings especially Decimal ("D") Format Specifier
If you need to convert a string to int use int.Parse or int.TryParse.
For the sake of completeness, if you have to use strings use String.PadLeft(8, '0'):
string numStr = "5";
String result = String.Format("ST{0}", numStr.PadLeft(8, '0'));
int number = 5; // put the number here
string result = $"ST{number:0000000#}";
// Or:
string result = $"ST{number:D8}";
This does exactly what you want.
EDIT: Keep in mind that this is only possible in C#6
You can do this like
string s = "215";
s = s.PadLeft(8, '0').PadLeft(9,'T').PadLeft(10,'S');
Use string.Format() together with a custom format string.

Format string to a 3 digit number

Instead of doing this, I want to make use of string.format() to accomplish the same result:
if (myString.Length < 3)
{
myString = "00" + 3;
}
If you're just formatting a number, you can just provide the proper custom numeric format to make it a 3 digit string directly:
myString = 3.ToString("000");
Or, alternatively, use the standard D format string:
myString = 3.ToString("D3");
string.Format("{0:000}", myString);
It's called Padding:
myString.PadLeft(3, '0')
This is how it's done using string interpolation C# 7
$"{myString:000}"
(Can't comment yet with enough reputation , let me add a sidenote)
Just in case your output need to be fixed length of 3-digit , i.e. for number run up to 1000 or more (reserved fixed length), don't forget to add mod 1000 on it .
yourNumber=1001;
yourString= yourNumber.ToString("D3"); // "1001"
yourString= (yourNumber%1000).ToString("D3"); // "001" truncated to 3-digit as expected
Trail sample on Fiddler https://dotnetfiddle.net/qLrePt
This is a short hand string format Interpolation:
$"{value:D3}"
"How to: Pad a Number with Leading Zeros"
http://msdn.microsoft.com/en-us/library/dd260048.aspx
Does it have to be String.Format?
This looks like a job for String.Padleft
myString=myString.PadLeft(3, '0');
Or, if you are converting direct from an int:
myInt.toString("D3");
You can also do : string.Format("{0:D3}, 3);

formatting string in C#?

I've a predefined string format. For instance '>>>,>>>,>>9.99' this means that the system should display string in this '500,000,000.10'. The format can change based on the users using it. How can I write a common function to display stings on the given format passing
the input value and the format as the parameter using C#
You can use the ToString method with a standard or custom format string
For example:
string format = "{0:000,000,000.00}";
string val = 12.3456;
Console.WriteLine(string.Format(format, value)); // it prints "000,000,123.23"
You can read more about formating values here http://www.csharp-examples.net/string-format-double/
decimal value = 1.2345;
string rounded = value.ToString("d2");
private string sDecimalFormat = "0.00";
decimal d = 120M;
txtText.Text = d.ToString(sDecimalFormat);
You could then have a setting for decimal format eg:
txtText.Text = d.ToString(Settings.DecimalFormat);
String.formate can be used for formating.
Go there if you want examples
http://www.csharp-examples.net/string-format-double/
I think the following might work:
String result = String.Format(fmt.Replace('>', '#').Replace('9', '0'), inpString);
fmt being the format you want to use and inpString being the string entered by the user.
Just replace the > with # and the 9 with 0 and it'll be a valid .Net formatstring.
There is a Format method on String.
String.Format("{0:X}", 10); // prints A (hex 10)
There are several methods to format numbers, date...
I dont seem to understand how you can make 500,000,000.10 from >>>,>>>,>>9.99' but I believe the answer would be
But I assume something you are looking for is: string.Format("500,000,00{0:0.##}", 9.9915)
You can then make a method like
Public string GetString(string Format, object value)
{
return string.Format(Format, value);
}
Something like this?

Categories