简体   繁体   中英

Double to String Format text format

i have the follwing lines of code

double formId=2013519115027601;
txtEditFormID.Text = formid.ToString();  

it gives me output

2.0135191150276E+15

if i write

txtEditFormID.Text = formId.ToString("0.0", CultureInfo.InvariantCulture);

it gives me

 2013519115027600.0

but i want the label text

2013519115027601

how to do it?

I don't have enough information about the usage of your formId variable.

As it is shown above it seems an error to use a double datatype when there is no decimals to work on. So redefining your variable as a long datatype will be easy and the conversion will be the same.

long formId=2013519115027601;
txtEditFormID.Text = formid.ToString();  

Not to mention the added benefit to your code to work with whole numbers instead of floating point numbers.
However, if you want to maintain the current datatype then

 txtEditFormID.Text = formId.ToString("R");

The Round Trip Format Specifier

When a Single or Double value is formatted using this specifier, it is first tested using the general format, with 15 digits of precision for a Double and 7 digits of precision for a Single. If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. If the value is not successfully parsed back to the same numeric value, it is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single.

Your first option is to use data type as long or decimal . Something else you can do if you want to keep using double is this :

        double formId = 2013519115027601;
        string text = formId.ToString();
        txtEditFormID.Text = text.Replace(".",string.Empty);  

this will remove all the '.' chars

There are times where I want calculations handled in double but I want the result displayed as as an int or even rounded amount, so the question isn't so strange (assuming that the given sample is simplified in order to ask the question).

I was going to post sample code for rounding, but it makes more sense to just use the built-in method Math.Round(). You can cast to a long, as mentioned above, but you won't have rounding, if desired (which it usually is, IMHO).

txtEditFormId.Text =(((long)formId).ToString();

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