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

Converting string "2 1/2" to double

Is there a C# class that will help convert strings that include fractions
( "2 1/2" e.g.) to doubles ? Convert.ToDouble() throws an invalid input
format exception.

Obviously I can write code to do it but this seems like a fairly normal
thing to want to do and I would expect C# to have already built in support.

Can anyone help? Even unmanaged code would do.

Brian Hough
Nov 17 '05 #1
5 2364
Nope. There's nothing in the Framework.

I wrote my own Fraction class, which I would be happy to share with
you. I'll send a message to you directly.

Nov 17 '05 #2
Looks like you've got an offer of code already but I'll give a hint to
anyone that wants to figure this out themselves.

using System.Text.RegularExpressions;

split the number into an integer and a fraction by using the space
between them, split the numerator and denominator by using the "/"
between them, divide the numerator by the denominator, then add the integer.

in Perl this could be done like so; (the regular expression will be
similar in C#, that is why I am showing you this)

my $fraction = "2 1/2";
my ($int, $num, $den) = $fraction =~ m|(\d+) (\d+)/(\d+)|;
print $int + ($num/$den);

this prints "2.5". In Perl variable types are pretty insignificant, its
smart enough to know when you need a double, and it auto converts. Perl
makes you LAZY, don't use it unless you need development speed.

Enough about Perl.

the bit between the "m|" and the "|" will be important to you.

jeremiah();

Valerie Hough wrote:
Is there a C# class that will help convert strings that include fractions
( "2 1/2" e.g.) to doubles ? Convert.ToDouble() throws an invalid input
format exception.

Obviously I can write code to do it but this seems like a fairly normal
thing to want to do and I would expect C# to have already built in support.

Can anyone help? Even unmanaged code would do.

Brian Hough

Nov 17 '05 #3
Can you send them to me to? Just concatenate the string to get my email
(trying to avoid spam)

"rene_el" + "" + "izondo@sbc" + "" + global.net"

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Nope. There's nothing in the Framework.

I wrote my own Fraction class, which I would be happy to share with
you. I'll send a message to you directly.

Nov 17 '05 #4
Hello!

Bruce Wood wrote:
[...snip...]
I wrote my own Fraction class, which I would be happy to share with
you. I'll send a message to you directly.


I've been looking for a decent "Fraction" class for a while now. Is there
any chance you'd send your code to me, too ?
Nov 17 '05 #5
Sent. Still waiting for e-mails from the others in order to send it to
them.

Nov 17 '05 #6

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

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never...
4
by: rainmaker1234 | last post by:
Its very simple in VC++. In the followeing code I have declared a String, and a double than I am taking the string and converting it into Double. getch() at the end is only to pause the screen so...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
5
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
10
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
3
by: nvx | last post by:
Hi, I'm looking for a simple way to convert a Double to a String exactly the .ToString() does, except these two differences: - the number of decimals is given (rounding is applied if necessary),...
2
by: Randy Smith | last post by:
Hi, Does anyone have any good techniques on how to convert an SQL Server 2005 "money" datatype into the ASP.Net "double" datatype? OR, should we be using "decimal" at both ends? TIA, Randy...
25
by: Blasting Cap | last post by:
I keep getting errors that pop up when I am trying to convert an application from dotnet framework 1.1 to framework 2.0. The old project was saved in sourcesafe from Visual Studio 2003, and I have...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
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,...
0
isladogs
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...

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.