简体   繁体   English

格式数字类型长为8位数字

[英]Format number type long into 8 digits

I have two variables of type long? 我有两个类型为long?变量long? . I need to use long1 variable and, if null, take long2. 我需要使用long1变量,如果为null,则使用long2。

//long1 format: 20130104
//long2 format: 2.01301071520553E16

var target = long1 ?? long2;

The variable target will be used in a query to find an exact match with a varibale in format "xxxxxxxx" (8 digits, same format as long1). 变量目标将在查询中用于查找格式为“ xxxxxxxx”(8位数字,与long1相同的格式)的变量的完全匹配。

Is there any elegant way to cut the long2 digits into the same format as long1 without having to parse it into a string first? 是否有任何优雅的方法可以 long2数字切割成与long1相同的格式,而不必先将其解析为字符串?

Since I will have to process the target variable as long and not a string, therefore I would avoid a second parse from string to long again. 由于我必须将目标变量处理为长而不是字符串,因此,我将避免再次进行从字符串到long的第二次解析。

Just take as many digits of accuracy as you require when you print the variable later on. 以后打印变量时,只需获得所需位数的精度即可。 It doesn't matter if it has 100 digits, it's only used for calculations. 100位数字无关紧要,仅用于计算。 When you are ready to display it use: 准备显示时,请使用:

String.Format("MyLong: {0:0.00000000}", long);

Or however you want it to look. 或者,但是您希望它看起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM