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,153 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now