473,387 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

removing decimal portion of a real number

I'm using VS2005 Beta 2 (C#) and would like to know how I can remove the
decimal portion (to the right of the decimal) of a real number. I want the
whole number portion but do not wish to discard the decimal portion; that
will be used for additional calcs.

Does C# have any methods for doing this?

Thanks,
Scott

Nov 17 '05 #1
4 17107
Create a temp int and cast the real number to it, or use the cast in an expression.

using System;

class CastToInt {

public static int Main(string[] args) {

double pi = 3.14159265358979;

int nPi = (int)pi;

Console.WriteLine("pi = {0} and pi cast to int = {1}", pi, nPi);

Console.WriteLine("pi = {0} and pi cast to int = {1}", pi, (int)pi);

Console.Write("\nPress Enter to continue...");

Console.Read();

return 0;

}

}

"Homer Simpson" <homer@moe's.com> wrote in message news:eU**************@TK2MSFTNGP12.phx.gbl...
I'm using VS2005 Beta 2 (C#) and would like to know how I can remove the
decimal portion (to the right of the decimal) of a real number. I want the
whole number portion but do not wish to discard the decimal portion; that
will be used for additional calcs.

Does C# have any methods for doing this?

Thanks,
Scott


Nov 17 '05 #2
Create a temp int and cast the real number to it, or use the cast in an expression.

using System;

class CastToInt {

public static int Main(string[] args) {

double pi = 3.14159265358979;

int nPi = (int)pi;

Console.WriteLine("pi = {0} and pi cast to int = {1}", pi, nPi);

Console.WriteLine("pi = {0} and pi cast to int = {1}", pi, (int)pi);

Console.Write("\nPress Enter to continue...");

Console.Read();

return 0;

}

}

"Homer Simpson" <homer@moe's.com> wrote in message news:eU**************@TK2MSFTNGP12.phx.gbl...
I'm using VS2005 Beta 2 (C#) and would like to know how I can remove the
decimal portion (to the right of the decimal) of a real number. I want the
whole number portion but do not wish to discard the decimal portion; that
will be used for additional calcs.

Does C# have any methods for doing this?

Thanks,
Scott


Nov 17 '05 #3
> Create a temp int and cast the real number to it, or use the cast in an
expression.

Agreed.

Another option is the Math.floor() function.

Greetings,
Wessel
Nov 17 '05 #4
> Create a temp int and cast the real number to it, or use the cast in an
expression.

Agreed.

Another option is the Math.floor() function.

Greetings,
Wessel
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tharma | last post by:
Hi I wanted to extract the decimal portion from a number. I tried the following code but I didn't get the exact decimal portion. If anyone know how to extract please let me know. $num =...
6
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there...
5
by: RichW | last post by:
Hi, I'm writing a program that has got a bit of maths in it. I perform a division and then end up with something like 2.50, or 3.87, or 4.9975 (ie. y.12345). I need to get rid of anything in...
0
by: Homer Simpson | last post by:
I'm using VS2005 Beta 2 (C#) and would like to know how I can remove the decimal portion (to the right of the decimal) of a real number. I want the whole number portion but do not wish to discard...
7
by: Oenone | last post by:
Can anyone explain why the following happens? \\\ Dim d1 As Decimal = CDec("100") Dim d2 As Decimal = CDec("100.00") MsgBox(d1.ToString) 'displays "100" MsgBox(d2.ToString) 'displays...
1
by: jmarr02s | last post by:
I am trying to change my Amount column Data Type from Integer to Decimal (precision 9 digits, scale 3, that is 6 digits to the left of decimal and 3 digits to the right of decimal. Here is the...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
13
by: Eps | last post by:
Hi there, I believe all strings in .net are unicode by default, I am looking for a way to remove all non ascii characters from a string (or optionally replace them). There is an article on...
10
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.