?
Solved

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

Posted on 2012-12-27
6
Medium Priority
?
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

801 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