473,378 Members | 1,427 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,378 software developers and data experts.

The value after the decimal point!

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 front of the decimal point so that
I'm just left with the bit after the decimal point, ie. 0.50, 0.87,
0.9975, 0.12345 for the examples above.

I can't find the function/method I need to do this. I've spent a
considerable time looking and haven't got much more time to be able to
spend on this. I know this is a basic thing to want to do because I
remember it being on my calculator from my schooldays!

In desperation, I'm now considering converting the value (ie. 3.87) to
a string, and then doing a regex thing to remove anything before the
decimal point, adding a 0 to the start of the string, and then
converting it back to a number.

Please can someone suggest the best way to achieve what I need to do.
(I'm sure it must be because I'm overlooking something simple!).

Thanks very much in anticipation.

Best wishes,
Rich
Nov 16 '05 #1
5 28247
Rich,

All you have to do is subtract .5 and then round the number to zero
decimal places. That will give you the integer portion of the number. You
can then just subtract that from the original number to get the decimal
portion. So, what you want is this:

// The number is in a variable named number, of type double.
// Get the integer portion.
double integerPortion = Math.Round(number - .5);

// Subtract from the original number.
double decimalPortion = number - integerPortion;

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"RichW" <ri************@zurich.com> wrote in message
news:c0**************************@posting.google.c om...
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 front of the decimal point so that
I'm just left with the bit after the decimal point, ie. 0.50, 0.87,
0.9975, 0.12345 for the examples above.

I can't find the function/method I need to do this. I've spent a
considerable time looking and haven't got much more time to be able to
spend on this. I know this is a basic thing to want to do because I
remember it being on my calculator from my schooldays!

In desperation, I'm now considering converting the value (ie. 3.87) to
a string, and then doing a regex thing to remove anything before the
decimal point, adding a 0 to the start of the string, and then
converting it back to a number.

Please can someone suggest the best way to achieve what I need to do.
(I'm sure it must be because I'm overlooking something simple!).

Thanks very much in anticipation.

Best wishes,
Rich

Nov 16 '05 #2
RichW wrote:

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 front of the decimal point so that
I'm just left with the bit after the decimal point, ie. 0.50, 0.87,
0.9975, 0.12345 for the examples above.

I can't find the function/method I need to do this. I've spent a
considerable time looking and haven't got much more time to be able to
spend on this. I know this is a basic thing to want to do because I
remember it being on my calculator from my schooldays!

In desperation, I'm now considering converting the value (ie. 3.87) to
a string, and then doing a regex thing to remove anything before the
decimal point, adding a 0 to the start of the string, and then
converting it back to a number.

Please can someone suggest the best way to achieve what I need to do.
(I'm sure it must be because I'm overlooking something simple!).

Thanks very much in anticipation.

Best wishes,

Rich


double value = 1.234;
value -= (int)value;
Nov 16 '05 #3
Hi Rich,

This should be faster than string manipulation:

double real = 3.87;
double fraction = real - Math.Floor(real);

This works for positive numbers, so you may want to look at Math.Ceiling for
negative numbers.

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com

"RichW" <ri************@zurich.com> wrote in message
news:c0**************************@posting.google.c om...
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 front of the decimal point so that
I'm just left with the bit after the decimal point, ie. 0.50, 0.87,
0.9975, 0.12345 for the examples above.

I can't find the function/method I need to do this. I've spent a
considerable time looking and haven't got much more time to be able to
spend on this. I know this is a basic thing to want to do because I
remember it being on my calculator from my schooldays!

In desperation, I'm now considering converting the value (ie. 3.87) to
a string, and then doing a regex thing to remove anything before the
decimal point, adding a 0 to the start of the string, and then
converting it back to a number.

Please can someone suggest the best way to achieve what I need to do.
(I'm sure it must be because I'm overlooking something simple!).

Thanks very much in anticipation.

Best wishes,
Rich

Nov 16 '05 #4
"Chrynne Schumptze" <Ch**************@discussions.microsoft.com> wrote in
message news:31**********************************@microsof t.com...
If you're using a float or double, if you cast it to an int, it will always round down.


To be strictly accurate, I believe it always "rounds toward zero" (which
for negative numbers is rounding up). In other words, it always truncates
the decimal portion, which is what we want.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 16 '05 #5
Thank you all very much for your responses - they give me lots of ways to do it!

Thanks again for your help - I really appreciate it.

Best wishes,
Rich
Nov 16 '05 #6

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
2
by: lkrubner | last post by:
I looked at the standard for UTF-8: http://www.ietf.org/rfc/rfc3629.txt It didn't say what decimal or hexidecimal values mapped out to what characters. Where do I find the list of...
3
by: JK | last post by:
This is an easy one, but I'm a JS newbie. I want to write a function that determines input value. If 0 - 100 than will use input as % (i.e., 3 would become .03 and used for calculations);...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
18
by: Kuljit | last post by:
I am doing Engineering(B.Tech) in Computer Science. I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the...
4
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi everyone! I'm using greece - greek in my control panel's regional options, and so, my decimal point is the comma (,), while it is the dot (.) for the sql server db, however, I'm facing...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.