I need to write values like:
9.6 x 10²
9.6 x 10¹²
I need to know if there is a way to format numbers as above in a string.
I need to write values like:
9.6 x 10²
9.6 x 10¹²
I need to know if there is a way to format numbers as above in a string.
As a follow up to my comment above - does something like this do what you require :
See : https://dotnetfiddle.net/dX7LAF
As per my comment above - the number is first converted to an exponential format string (https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#EFormatString), that string is then split on the exponential separator 'E'. The first array is the numeric part, the second the power of 10 to which it is raised - this is converted to superscript characters using one of the techniques of the link I gave (Convert a string/integer to superscript in C#), converted back to a string & the two parts combined using "x 10" as the new separator.
I have assumed you want the value to single digit precision as per your example with no preceding + sign. If you need anything else you could pass the format as a parameter. The code for superscript + is '\u207A'. There is a link here (at the time of writing) giving the list of superscript codes : http://unicode.org/charts/PDF/U2070.pdf
Try this:
As a test case run
with the output:
By no means optimized, but it does the job. The exponents are in engineering form (multiples of 3 only, in order to avoid things like
10¹
). As a bonus, the number can be formatted to a specific number of significant digits by supplying a format code likeg4
org5
for 4 or 5 digits respectively.NAN
orInf
.You have to find the appropriate character from the code page you are using, for example UTF-8:
There is no such thing as formatting in a string, it is just all data.