Solved

Limit a float value to two decimal places Before it is changed to a string and called by an asp:literal

Posted on 2008-06-19
6
2,161 Views
Last Modified: 2013-12-16
I am new to C#, so I need to limit a string or float to one decimal places...

 float grossYearly = System.Convert.ToSingle(txtField1.Text.ToString());      
float newGross = grossYearly/12;      
float monthly = System.Convert.ToSingle(txtField2.Text.ToString());                
float total = monthly / newGross;              
float newTotal = total*100;                  
string i = newTotal.ToString();
                        Panel2.Visible = false;
                        Panel1.Visible = true;
 Literal1.Text=i;


then it is called here...
<asp:Literal id="Literal1" runat="server"></asp:Literal>%

I know there is a simple answer, but there are a lot of issues with testing here, so I need to be sure...Thanks.
0
Comment
Question by:theclassic
  • 3
  • 2
6 Comments
 

Author Comment

by:theclassic
ID: 21822757
Sorry, I misprinted the title, it should read one decimal place....
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21822845
Use a format string.

2 decimal places:
string i = newTotal.ToString("F2");

1decimal place:
strign = i newTotal.ToString("F1");

Standard format strings:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Custom format strings:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21822924
Do you care if the number is rounded up?
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:theclassic
ID: 21823021
It just needs to be rounded, as in 1.35 would be 1.4....
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 50 total points
ID: 21823066
Do this then:
string i = newTotal.ToString("0.0");
0
 

Author Comment

by:theclassic
ID: 21823160
Thank you so much !
0

Featured Post

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.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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