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 5 28264
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(numb er - .5);
// Subtract from the original number.
double decimalPortion = number - integerPortion;
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"RichW" <ri************ @zurich.com> wrote in message
news:c0******** *************** ***@posting.goo gle.com... 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
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;
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.goo gle.com... 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
"Chrynne Schumptze" <Ch************ **@discussions. microsoft.com> wrote in
message news:31******** *************** ***********@mic rosoft.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!)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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. "Decimal Number" sometimes serves to distinguish Base 10
numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal
Number" serves to differentiate a number from an integer. For the rest of this
post I shall only use either...
|
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 characters in UTF-8, and their
hexidecimal value?
|
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); otherwise, as dollar value (i.e.,
1,000 would become $1,000 and used for calculations). In other words, I want
input.value 3 to either be input.value 1 * input.value 2, or simply =
input.value 1. Please be specific about placement of function (header or...
|
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 believe that. in pieces
of the application where speed really matters you can still use "normal"
functions or even static methods which is basically the same.
in C there arent the simplest things present like constants, each struct and
enum...
| |
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 parent table.
----------------------------
|
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
number of digits in a given number.
I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).
|
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 trouble when I need to parse the sql server decimal as
an asp.net double!
For example:
|
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'
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |