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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -