[英]Append/Concat multiple ushort values
I am working on C# application (.Net 4.0). 我正在使用C#应用程序(.Net 4.0)。 At some situation I want to append the multiple ushort values as mentioned below
在某些情况下,我想附加多个ushort值,如下所述
Ushort a = 123;
Ushort b = 045;
Ushort c = 607;
Ushort d = 008;
And I want the result as 12304560700. 我希望结果为12304560700。
Currently with below approach 目前采用以下方法
var temp = Convert.ToString(a) + Convert.ToString(b) + Convert.ToString(c) + Convert.ToString(d);
I am getting the temp value as 123456078. 我得到的临时值为123456078。
I do understand that because of ushort datatype it eliminate all the leading zero. 我确实知道,由于ushort数据类型,它消除了所有前导零。 But I am expecting the result as 12304560700.
但我期望结果为12304560700。
I may have made the use of padleft method but the length and leading zero counts are not fix, so that option also doesn't suits my requirement. 我可能已经使用padleft方法,但是长度和前导零计数不固定,因此该选项也不适合我的要求。
I would like to know how I can achieve the same, any small inputs on the same is also greatly appreciated. 我想知道如何实现相同的目标,对同一目标的任何小投入也将不胜感激。
Thanks in advance. 提前致谢。
You want all your numbers to be formatted using 3 digits, with leading zeros if needed. 您希望所有数字都使用3位数字格式,如果需要,请使用前导零。 Looking at Standard Numeric Format Strings you get this:
查看标准数字格式字符串,您会得到以下信息:
var temp = a.ToString("D3") + b.ToString("D3") + ...
int a = 123;
int b = 045;
int c = 607;
int d = 008;
why cant u do it this way? 你为什么不能这样呢? wont the result be what u want?
结果不会是您想要的吗?
int temp = a.ToString() + b.ToString() + c.ToString() + d.Substring(d.Length - 2);
result as 12304560700. 结果为12304560700。
ushorts
are not capable of storing numbers with leading zeros. ushorts
无法存储前导零的数字。 In its eyes, 45
and 045
are the same exact number. 在它的眼中,
45
和045
是相同的精确数字。
I recommend you to just store the numbers as strings, like this: 我建议您仅将数字存储为字符串,如下所示:
var a = "123";
var b = "045";
var c = "607";
var d = "008";
This is especially easy to do if you are getting these things from the console. 如果要从控制台获取这些内容,则这特别容易做到。
Instead of assigning values to ushort assign it to string or var. 而不是将值分配给ushort,而是将其分配给string或var。
eg: string a="123"; 例如:string a =“ 123”; string b="011" etc or var a="123" etc
字符串b =“ 011”等或var a =“ 123”等
then var temp = a+b+... this will work. 然后var temp = a + b + ...这将起作用。 No Ushort value store data leading with Zero.
没有Ushort值存储以零开头的数据。 Thank you
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.