c# - Replace double String.Format with string interpolation -
i tried migrate line of code uses string.format
twice new .net framework 6 string interpolation feature until not successfull.
var result = string.format(string.format("{{0:{0}}}{1}", strformat, withunit ? " kb" : string.empty), (double)filesize / filesizeconstant.ko);
a working example be:
var result = string.format(string.format("{{0:{0}}}{1}", "n2", " kb"), 1000000000 / 1048576d);
which outputs: 953,67 kb
is possible or need use old construct special case?
the main issue lies in strformat
variable, can't put format specifier "{((double)filesize/filesizeconstant.ko):strformat}"
because colon format specifier not part of interpolation expression , not evaluated string literal n2
. documentation:
the structure of interpolated string follows:
$"<text> { <interpolation-expression> <optional-comma-field-width> <optional-colon-format> } <text> ... } "
can make format part of expression passing double.tostring
method:
$"{((double)filesize/filesizeconstant.ko).tostring(strformat)}{(withunit?" kb":string.empty)}";
Comments
Post a Comment