Solved

ASP.NET - C# - Format string to numeric value

Posted on 2012-12-27
6
392 Views
Last Modified: 2012-12-27
I have a value I am retrieving from a database.  I am assigning the value to a text box on my asp.net form.  The field in the database is a money value.  I want the below result.

Database Value     Result Value
0.000000                0.00

I have tried the following

string s = string.Format("{0:c}", rdr["myValue"].ToString());
this.myTextBox.text = s;

and

string s = string.Format("{0:#,0.00}", rdr["myValue"].ToString());
this.myTextBox.text = s;

But neither works.

Any ideas?
0
Comment
Question by:CipherIS
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 38724706
what do you get? what is the value of s?

you can also try string.Format("{0:C}", rdr["myValue"].ToString());
0
 
LVL 22

Expert Comment

by:p_davis
ID: 38724712
are you just getting 0?
0
 
LVL 22

Expert Comment

by:p_davis
ID: 38724718
String.Format("{0:.##}", rdr["myValue"].ToString());
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:CipherIS
ID: 38724788
@p_davis - I'm getting "0.0000" as a result for
string s = string.Format("{0:c}", rdr["myValue"].ToString());

I tried string s = string.Format("{0:C}", rdr["myValue"].ToString());
and still getting "0.0000"
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38724794
You cannot call ToString and use a numeric format at the same time. Cast the thing returned from the database to a numeric type; then apply the format.

e.g.

string s = string.Format("{0:c}", (double)rdr["myValue"]);

Open in new window

0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 38724822
Thanks kaufmed!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question