/    Sign up×
Community /Pin to ProfileBookmark

float or double to string

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

to post a comment
Full-stack Developer

9 Comments(s)

Copy linkTweet thisAlerts:
@buntineApr 29.2005 — 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.
Copy linkTweet thisAlerts:
@DiedtjeauthorApr 29.2005 — I'm working .NET

I just want to convert a double precision to a string.
Copy linkTweet thisAlerts:
@buntineApr 30.2005 — Ok, try asking in the .NET forum.
Copy linkTweet thisAlerts:
@JuuitchanApr 30.2005 — 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.
<i>
</i>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:
<i>
</i>int count=0; double total=0;
for (count=1; count&lt;=8; count++) total+=0.1;
// total will equal 0.8000000000000001
Copy linkTweet thisAlerts:
@JuuitchanApr 30.2005 — If you want to see numbers with commas, I suggest you first convert the numbers to strings, then replace all decimal points with commas.
Copy linkTweet thisAlerts:
@ray326Apr 30.2005 — If you want to see numbers with commas, I suggest you first convert the numbers to strings, then replace all decimal points with commas.[/QUOTE]
Are you saying there is no concept of a locale in C#?
Copy linkTweet thisAlerts:
@JuuitchanApr 30.2005 — 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?
Copy linkTweet thisAlerts:
@ray326Apr 30.2005 — 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.
Copy linkTweet thisAlerts:
@DiedtjeauthorMay 02.2005 — 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";
×

Success!

Help @Diedtje spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...