I currently have the following longs in a small C# program.
long one = 1;
long two = 1005;
long three = 100000005;
long four = 1111112258552;
I want to format them in a string so that they are a thousandth of the size without dividing the values by a thousand.
Example
Input : Output
1 0.001
1005 1.005
100000005 100000.005
1111112258552 1111112258.552
I have tried string formats such as {0:0,000} and {0:0.000} but neither provided the result I was after.
How can I achieve the result I am after? Any tips or pointers would be appreciated
Some sample code
long one = 1;
long two = 1005;
long three = 100000005;
long four = 1111112258552;
string format = "{0:0,000}";
string s1 = String.Format(format, one);
string s2 = String.Format(format, two);
string s3 = String.Format(format, three);
string s4 = String.Format(format, four);
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
Console.WriteLine(s4);
Try "{0:0,.000}" for your format.
long one = 1;
long two = 1005;
long three = 100000005;
long four = 1111112258552;
string format = "{0:0,.000}";
string s1 = String.Format(format, one);
string s2 = String.Format(format, two);
string s3 = String.Format(format, three);
string s4 = String.Format(format, four);
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
Console.WriteLine(s4);
Console output:
0.001
1.005
100000.005
1111112258.552
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#the--custom-specifier-2
Related
This is an extension of the question asked here:
Converting string to int and then back to string
I am now converting a range of string values to add all of them together. I obviously cannot do something like:
int Total;
string str1 = "1";
string str2 = "1";
string str3 = "1";
string str4 = "1";
string str5 = "1";
string str6 = "1";
...
...
void Start()
{
Total = int.Parse(str1) + int.Parse(str2) + .....
}
I am looking for a way to parse all the strings into int such that:
Total = int.Parse(str1 + str2 + str3 + ...)
How do I achieve this?
As long as you do not know how many values you will have, you cannot hardcode all the single values into independent variables.
Advice: You should really consider storing the data in the correct format, You approach should be working with integers instead of strings (using a List<int>). The code would be more bug resistant and more simple.
int Total = 0;
List<string> valuesToAdd = new List<string>(){
"1",
"4",
"1",
"99"
};
void Start()
{
Total = 0;
foreach(string stringValue in valuesToAdd)
{
Total += int.Parse(stringValue);
}
}
Does using string.Format like this:
string.Format(txt, arg0, arg1, arg2, arg3, arg4,...)
is the same as saying txt+=args; thus it creates new object every time it append a new string from the args list? and better using StringBuilder.Format ?
My comment above means that you should test your performances in your environment. Use a Stopwatch instance, create a loop that runs over the string.Format and StringBuilder.AppendFormat for at least one hundred thousands times and then measure the value in the Stopwatch.ElapsedMilliseconds. This will roughly give you an idea of the differences.
In mine environment the two approaches are pretty identical. The difference on a 100000 loop is 2/3 milliseconds advantage for StringBuilder but the morale is:
DO NOT DO MICROOPTIMIZATIONS
(unless you have absolutely clear that the result worth the effort).
Sample:
string s1 = "Argument 1";
string s2 = "Argument 2";
string s3 = "Argument 3";
string s4 = "Argument 4";
string s5 = "Argument 5";
string s6 = "Argument 6";
string s7 = "Argument 7";
string s8 = "Argument 8";
string s9 = "Argument 9";
string result = string.Empty;
object[] data = new object[] { s1, s2, s3, s4, s5, s6, s7, s8, s9 };
Stopwatch sw = new Stopwatch();
sw.Start();
for(int x = 0; x < 100000; x++)
result = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}", data);
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
StringBuilder sb = new StringBuilder();
sw = new Stopwatch();
sw.Start();
for (int x = 0; x < 100000; x++)
{
sb.Length = 0;
sb.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7},{8}", data);
result = sb.ToString();
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
If you use just string format, use string.Format.
If you use a lot concatenation of strings and/or string forms, use StringBuilder.
Check this out:
https://support.microsoft.com/en-us/kb/306822
String.Format uses a StringBuilder in its implementation, So you cannot say which one is better.
I am having to loop throw 00.000000000000001 to 100 however whenever I do this and try and show it as a string it goes to numbers like 2E-15 and stuff, I have a feeling this is just how its represented however how can I get the actual number.
You can use String.Format to determine how your double is displayed:
double d1 = 00.000000000000001;
double d2 = 00.0001;
string text1 = String.Format("{0:000.000000000000000}", d1);
string text2 = String.Format("{0:000.###############}", d1);
string text3 = String.Format("{0:000.000000000000000}", d2);
string text4 = String.Format("{0:000.###############}", d2);
Console.WriteLine(text1+" - "+text2);
Console.WriteLine(text3+" - "+text4);
see MSDN - Custom Numeric Format strings
You could just use decimal type instead of double, try this code below:
using System;
class test
{
static void Main()
{
for(decimal i = 0.000000000000001m; i < 100; i += 0.000000000000001m)
{
Console.WriteLine(i);
}
}
}
From following string String1 I want to remove everything before "am" and everything after "Uhr"
string String1 = "Angebotseröffnung am 27.03.2014, 11:00 Uhr, Ort: Vergabestelle, siehe a).";
So at the end I have this string. "am 27.03.2014, 11:00 Uhr".
I am using this code but I know this is not a good approach. Can some one help me with better options.
String1 = String1.Replace("Angebotseröffnung", "");
String1 = String1.Replace("Ort", "");
String1 = String1.Replace("Vergabestelle", "");
String1 = String1.Replace("siehe", "");
String1 = String1.Replace("a)", "");
Try something like this:
var string1 = "Angebotseröffnung am 27.03.2014, 11:00 Uhr, Ort: Vergabestelle, siehe a).";
var startIndex = string1.IndexOf("am"); // 18
var endIndex = string1.IndexOf("Uhr") + 3; // 42
// Get everything between index 18 and 42
var result = string1.Substring(startIndex, endIndex - startIndex);
// result: "am 27.03.2014, 11:00 Uhr"
Another option is to use Regex.Match.
string output = Regex.Match(String1, "am.*Uhr").Value;
But it will work only if you definitely have am and Uhr in your string.
Depending on your input you may require am.*?Uhr or (?:a|p)m.*?Uhr regex.
If the format is strict and always contains Angebotseröffnung am and Uhr this is the most efficient:
string String1 = "Angebotseröffnung am 27.03.2014, 11:00 Uhr, Ort: Vergabestelle, siehe a).";
string result = null;
string startPattern = "Angebotseröffnung am ";
int start = String1.IndexOf(startPattern);
if (start >= 0)
{
start += startPattern.Length;
string endPattern = " Uhr";
int end = String1.IndexOf(endPattern, start);
if (end >= 0)
result = String1.Substring(start, end - start + endPattern.Length);
}
if you know that the format will always be the same =>
var r = new Regex("am \d{1,2}.\d{1,2}.\d{4} Uhr");
var result = r.Match(input);
With StringBuilder, Replace works the same way as with strings but it doesn't need its result to be assigned
class Program
{
static void Main()
{
const string samplestring = "hi stack Over Flow String Replace example.";
// Create new StringBuilder from string
StringBuilder str = new StringBuilder(samplestring );
// Replace the first word
// The result doesn't need assignment
str.Replace("hi", "hello");
Console.WriteLine(b);
}
}
Output Will be -->
hello stack Over Flow String Replace example
if the string will always contain am and Uhr, and will always have am before Uhr:
string String1 = "Angebotseröffnung am 27.03.2014, 11:00 Uhr, Ort: Vergabestelle, siehe a).";
int indexOfAm = String1.IndexOf("am");
int indexOfUhr = String1.IndexOf("Uhr");
string final = String1.Substring(indexOfAm, (indexOfUhr + 3) - indexOfAm);
Try this
String1 = String1.Substring(String1.IndexOf("am"), (String1.IndexOf("Uhr") - String1.IndexOf("am"))+3);
I want to eliminate B if i scan Serial number bar code: 21524116476CA2006765B
Output: 21524116476CA2006765
string foo = "21524116476CA2006765B";
string bar = foo.Substring(0, Math.Max(foo.Length - 1, 0));
If you can't be certain that the input barcode will always end with a B, do something like this:
char[] barcodeEnd = { 'B' };
string input = "21524116476CA2006765B";
string output = input.TrimEnd(barcodeEnd);
PUt the first line somewhere it'll only get run once, so you aren't constantly creating new char[] arrays. You can add more characters to the array if you're looking for other specific characters.
string s = "21524116476CA2006765B";
string b = s.Substring(0, s.Length - 1);
string value= "21524116476CA2006765B";
string bar = value.Substring(0, 19);
string str = "Your String Whatever it is";
str = str.Substring(0, s.Length - 1);