Double to string, keeping trailing zero

I had this problem and I don’t think that for a better solution I tried to convert the double value to a string and replace () with ',' by '.'. This works well, but only when the ending digits are not zero, do I need zeros in my line, even if the value is 1234.0. This worked well for decimal values. I tried to convert double to decimal, but I lost decimal digits from zeros.

[EDIT] Sorry, forgot to mention C # .NET

I know something is missing. I would appreciate some suggestions.

thank

+3
source share
3 answers

It will depend on the language. Example in C #

d.ToString("0.00");

2 ​​ , ( ).

+9

... Fortran - :: -)

      write(*,110) x
  110 format (F5.3)

(, , ...)

+1

If it is in Java, check out the NumberFormat class method setMinimumFractionDigits().

Example:

double d1 = 2.5;
double d2 = 5.0;

NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);

String d1s = nf.format(d1);
String d2s = nf.format(d2);

System.out.println("d1s: " + d1s + " and d2s: " + d2s);

produces

d1s: 2.50 and d2s: 5.00

0
source

Source: https://habr.com/ru/post/1735619/


All Articles