473,327 Members | 2,016 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,327 software developers and data experts.

convert double to string in fraction format( 0.75 to 3/4) and vise versa

hi all.
In my program I need to convert a double number to stringrepresentation in fraction format and vise versa. For example,convert 0.75 to string "3/4" and convert string "1/2" to 0.5.The class will only need to handle numbers that increment by1/32. That is 1/32, 2/32, 3/32.... 31/32, 1. If the doublenumber is 0.28 which is between 1/4 and 9/32, the string shouldbe 9/32(go with the bigger number).

TIA

--------------------------------
From: paul gao

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>ReuzASqUkECHu6connty0g==</Id>
Nov 16 '05 #1
2 11470
Paul,

quite simple math --

1. multiple the double by the base - 32 in your case, to get the numerator.

2. set the denominator to 32.

3. use Euclid's algorithm to find GCD.

4. divide numerator and denominator by GCD...

See Knuth, Vol1, page 2 for details of Euclid's algorithm - it is a trivial
iterative search method
here is quick hack in case you don't have Knuth handy...

regards
roy fine
namespace GCDExample{
class Class1{
static int GCD(int numer, int denom){
while(true){
int rem = denom%numer;
if(rem == 0) return numer;
denom = numer;
numer = rem;
}
}
static void RunTest(double dbVal){
int numerator = (int)(dbVal * 32);
int denominator = 32;
int gcd = GCD(numerator,denominator);
numerator /= gcd;
denominator /= gcd;
Console.WriteLine("double val:{0}\n numerator: {1}\n denominator:
{2}\n\n",dbVal,numerator,denominator);
}
static void Main(string[] args){
RunTest(0.0625);
RunTest(0.09375);
RunTest(0.25);
RunTest(0.50);
RunTest(0.75);
RunTest(0.9375);
RunTest(0.96873);
RunTest(0.96874);
RunTest(0.96875);
RunTest(0.96876);
RunTest(0.96877);
RunTest(0.96878);
RunTest(0.96879);
RunTest(0.96880);
}
}
}
"paul gao via .NET 247" <an*******@dotnet247.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
hi all.
In my program I need to convert a double number to string representation in
fraction format and vise versa. For example, convert 0.75 to string "3/4"
and convert string "1/2" to 0.5. The class will only need to handle numbers
that increment by 1/32. That is 1/32, 2/32, 3/32.... 31/32, 1. If the double
number is 0.28 which is between 1/4 and 9/32, the string should be 9/32(go
with the bigger number).

TIA

--------------------------------
From: paul gao

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>ReuzASqUkECHu6connty0g==</Id>
Nov 16 '05 #2
Paul

note - the algorithm works for positive integers - you must specifically
handle 0 and negative values.

rlf
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
Paul,

quite simple math --

1. multiple the double by the base - 32 in your case, to get the numerator.
2. set the denominator to 32.

3. use Euclid's algorithm to find GCD.

4. divide numerator and denominator by GCD...

See Knuth, Vol1, page 2 for details of Euclid's algorithm - it is a trivial iterative search method
here is quick hack in case you don't have Knuth handy...

regards
roy fine
namespace GCDExample{
class Class1{
static int GCD(int numer, int denom){
while(true){
int rem = denom%numer;
if(rem == 0) return numer;
denom = numer;
numer = rem;
}
}
static void RunTest(double dbVal){
int numerator = (int)(dbVal * 32);
int denominator = 32;
int gcd = GCD(numerator,denominator);
numerator /= gcd;
denominator /= gcd;
Console.WriteLine("double val:{0}\n numerator: {1}\n denominator:
{2}\n\n",dbVal,numerator,denominator);
}
static void Main(string[] args){
RunTest(0.0625);
RunTest(0.09375);
RunTest(0.25);
RunTest(0.50);
RunTest(0.75);
RunTest(0.9375);
RunTest(0.96873);
RunTest(0.96874);
RunTest(0.96875);
RunTest(0.96876);
RunTest(0.96877);
RunTest(0.96878);
RunTest(0.96879);
RunTest(0.96880);
}
}
}
"paul gao via .NET 247" <an*******@dotnet247.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
hi all.
In my program I need to convert a double number to string representation in fraction format and vise versa. For example, convert 0.75 to string "3/4"
and convert string "1/2" to 0.5. The class will only need to handle numbers that increment by 1/32. That is 1/32, 2/32, 3/32.... 31/32, 1. If the double number is 0.28 which is between 1/4 and 9/32, the string should be 9/32(go
with the bigger number).

TIA

--------------------------------
From: paul gao

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>ReuzASqUkECHu6connty0g==</Id>

Nov 16 '05 #3

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

Similar topics

3
by: jeff_zhang446 | last post by:
Hi, I try to convert double to string as below: std::string cnvrtToString(double lValue) { std::ostringstream lStream; lStream << lValue; return lStream.str();
2
by: Laurence | last post by:
I want to convert a double value to string. 1.2345678911 -> "1.234567891" 123 -> "123" I used the following code to convert it: string str = doubleValue.ToString("f9"); but the second...
5
by: Jeff | last post by:
I am trying to crete a method that will convert an improper fraction to a mixed number... I am not sure how about how to acomplish this. I know I can get the remainder with the modulus operator...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
8
by: shiniskumar | last post by:
Ive got a double variable dTotal =5.037717235E7 i formatted it using deciFormat.format(dTotal) and got the String value of dTotal=50377172.35 now i have to pass this value as an argument to a...
0
by: aparna12 | last post by:
Hi all Please tell me how to Convert double to string in fraction format (.75 to 3/4) in asp.net. Thanks Aparna
21
by: Aman JIANG | last post by:
hi I need to do this (convert double to string) fast, safe and portable. Is there any way to do this ? Except the ways following: 1. C++ I/O stream, stringstream (and boost::lexical_cast) 2....
2
by: yogi_bear_79 | last post by:
I have a double of unknown length that I need to split at the decimal. I thought I would convert it either to a string or a char. char seems to be the best since it easily lends itself to...
1
by: Bjorn Brox | last post by:
Hi! In germany, norway and France(?) we are using ',' as decimal separator and it always messes up when you convert a double to and from a string where the interface expects double values stored...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.