I need to keep culture when converting double to string and also round to only one decimal place.
Converting double to string with culture:
((12275454.8).ToString("N", new CultureInfo("sl-SI")));
Gives output:
12.275.454,80
Converting double to string with only one decimal:
string.Format("{0:F1}",12275454.8)
Gives output:
12275454.8
The second output is without culture, the first output is not rounded to one decimal place. How to combine both methods?
Just use the format string of your second example in your first example, ie:
((12275454.8).ToString("N1", new CultureInfo("sl-SI")));
Edit: Changed format from F1 to N1 as per request. The difference between both is that N additionally uses thousands separators, whereas F does not. For details see https://msdn.microsoft.com/en-US/library/dwhawy9k(v=vs.110).aspx
You can set "sl-SI"
culture as a default one:
using System.Threading;
...
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("sl-SI");
string test = string.Format("{0:F1}",12275454.8);
Add try..finally
if you want "sl-SI"
culture for a block of code only:
var savedCulture = Thread.CurrentThread.CurrentCulture;
try {
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("sl-SI");
// Let's work with "sl-SI" for a while
string test = string.Format("{0:F1}",12275454.8);
...
}
finally {
Thread.CurrentThread.CurrentCulture = savedCulture;
}
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.