Solved

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

Posted on 2012-12-27
6
391 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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