Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom - 06 Feb 2008 18:16 GMT
I have this

131.988

I want
132

how can I accomplish this?
Mark Rae [MVP] - 06 Feb 2008 18:34 GMT
>I have this
>
[quoted text clipped - 4 lines]
>
> how can I accomplish this?

double MyDouble = 131.988;
string MyFormattedDouble = MyDouble.ToString("0");

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Angel - 06 Feb 2008 18:40 GMT
Your question should be more to the point.  Usually a piece of code helps us
give you better answers.  

But here it goes

dim value decimal = 131.987
label1.text = format(value, "#0")

will give you 132

Signature

aaa

> I have this
>
[quoted text clipped - 4 lines]
>
> how can I accomplish this?
Mark Moeykens - 06 Feb 2008 19:34 GMT
If you are looking to just round, you can convert the number to an integer as
well.

VB.NET
Dim decimalNumber As Decimal = 131.988
Dim intNumber As Integer = decimalNumber ' intNumber will be 132
or you can show you are converting it (use one of these methods depending on
how big your integer can get)
Dim intNumber As Integer = Convert.ToInt16(doubleNumber)
Dim intNumber As Integer = Convert.ToInt32(doubleNumber)
Dim intNumber As Integer = Convert.ToInt64(doubleNumber)

or if your starting number is a double:

Dim doubleNumber As Double = 131.988
Dim intNumber As Integer = doubleNumber

C#
double doubleNumber = 131.988;
int integerNumber = Convert.ToInt16(doubleNumber);

or depending on how big your integer will get you can also use:
int integerNumber = Convert.ToInt32(doubleNumber);
int integerNumber = Convert.ToInt64(doubleNumber);

Is your starting number of 131.988 a string?
You can also use (VB.NET):
Dim doubleString As String = "131.988"
Dim intNumberFromString As Integer = doubleString

> I have this
>
[quoted text clipped - 4 lines]
>
> how can I accomplish this?
Phil H - 06 Feb 2008 23:13 GMT
> I have this
>
[quoted text clipped - 4 lines]
>
> how can I accomplish this?

Microsoft help documentation does not specify the precise mathematical
operation that is used when format specifiers convert decimal non-
integral numbers to integer strings. So string formatting cannot be
relied upon as means of rounding.

The System.Math class on the other have a function Round which
conforms to an IEEE standard and so can be used reliably for this
purpose.

Thus

double x = 131.988
int y = Math.Round(x)

will yield a properly rounded integer result (whatever value x happens
to be), which can then be converted to a string if desired.

This is important because there are various regimes that can be used
when the decimal lies mid way between two integers e.g. what do you do
in the case of 131.5 ? According to the IEEE standard if the lower
integer is odd then round up otherwise round down (hence 131.5 rounds
to 132 whereas 132.5 rounds to 132 rather than 133).

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.