Click to See Complete Forum and Search --> : float or double to string


Diedtje
04-29-2005, 09:12 AM
I have a float or double variable.
I want the digits te appear in a label.
But I want 2 digits behind the comma.
If I just use the toString() method It will not give any digits behind the comma. Just 1 or 2 and not 1,35 or 2,69
What do i need to do?

thx

buntine
04-29-2005, 09:59 AM
I am assuming you are working with Java. In which case, you need to ask in the Java forum from now on. In a float or a double, there should be a dot, rather than a comma. e.g. 2.69. Also, double and float are primitive types in Java, so they do now have a toString() method.

In any case, which language are you actually using?

Regards.

Diedtje
04-29-2005, 10:07 AM
I'm working .NET

I just want to convert a double precision to a string.

buntine
04-29-2005, 09:31 PM
Ok, try asking in the .NET forum.

Juuitchan
04-29-2005, 09:40 PM
Diedtje:

In every programming language that I know of that supports floats and doubles, you need to use a decimal point.

Here is an example from Java. I haven't used Java in years, so this might be wrong.

double a=0.75;
double b=3.5;
double c=a+b;
// at this point, c will equal 4.25


Please post your code.

Please be aware that arithmetic with doubles can contain small errors.
Try this:

int count=0; double total=0;
for (count=1; count<=8; count++) total+=0.1;
// total will equal 0.8000000000000001

Juuitchan
04-29-2005, 09:42 PM
If you want to see numbers with commas, I suggest you first convert the numbers to strings, then replace all decimal points with commas.

ray326
04-30-2005, 12:18 AM
If you want to see numbers with commas, I suggest you first convert the numbers to strings, then replace all decimal points with commas.
Are you saying there is no concept of a locale in C#?

Juuitchan
04-30-2005, 04:10 PM
I don't know. Is there?

If you use commas as decimal points, how do you separate arguments to a function or items in a list?

ray326
04-30-2005, 05:48 PM
AFAIK commas are always used as list item separators. I've also never seen a programming language implementation that used commas for decimal points in its source but I've got a very narrow world view on that.

Diedtje
05-02-2005, 02:20 AM
for those who care about the solution:

pic.length = the filesize of the pic. Is a FileInfo property

double size;
size = Convert.ToDouble(pic.Length) / 1048576;
lblSize.Text = string.Format("{0:f}", size) + " MB";