简体   繁体   中英

Format number type long into 8 digits

I have two variables of type long? . I need to use long1 variable and, if null, take 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).

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?

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.

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. When you are ready to display it use:

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

Or however you want it to look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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