Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double string.format

Tags:

I have some double values I want to convert to a string with this pattern:

0.xx or x.xx 

Currently I have try this:

double.ToString("#.#0"); 

What should I add in order to see zero in case my number start with zero?

like image 921
falukky Avatar asked Mar 26 '12 09:03

falukky


People also ask

What is double formatting?

Double-precision floating-point format (sometimes called FP64 or float64) is a computer number format, usually occupying 64 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point.

What is .2f in Java?

printf("%. 2f", value); The %. 2f syntax tells Java to return your variable (value) with 2 decimal places (.

What is %s in string format?

The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value. The following Python code illustrates the way of performing string formatting.


2 Answers

just use

myDouble.ToString("0.00") 

that should do the trick

like image 61
Random Dev Avatar answered Sep 28 '22 08:09

Random Dev


myDouble.ToString("N2") 

should also work.

Have a look at

MSDN: Custom Numeric Format Strings

MSDN: Standard Numeric Format Strings

like image 32
raznagul Avatar answered Sep 28 '22 08:09

raznagul