473,769 Members | 6,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 stringrepresent ation 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>ReuzASqUkEC Hu6connty0g==</Id>
Nov 16 '05 #1
2 11527
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,d enominator);
numerator /= gcd;
denominator /= gcd;
Console.WriteLi ne("double val:{0}\n numerator: {1}\n denominator:
{2}\n\n",dbVal, numerator,denom inator);
}
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*******@dotn et247.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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>ReuzASqUkEC Hu6connty0g==</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.obf uscate.net> wrote in message
news:em******** ******@tk2msftn gp13.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,d enominator);
numerator /= gcd;
denominator /= gcd;
Console.WriteLi ne("double val:{0}\n numerator: {1}\n denominator:
{2}\n\n",dbVal, numerator,denom inator);
}
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*******@dotn et247.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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>ReuzASqUkEC Hu6connty0g==</Id>

Nov 16 '05 #3

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

Similar topics

3
2315
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
27319
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 value is converted to "123.000000000".
5
5771
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 (%), but I am not sure how to get the quotent. any insight would be appreciated. thanks
15
10833
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
3943
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 function convert(double num); how can i convert the string value 50377172.35 to double?
0
1182
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
7518
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. sprintf (and its brothers) 3. any nonstandard C/C++ functions
2
2639
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 splitting. I have a few issues with this theory. Hard codeing char M works fine for my test double -0.5 But I would like it to be more versitile. The format "%2.2f" works fine with my test data, but that should also be versitile. inline void...
1
4079
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 as string is using '.' What parameter shall I use in double.ToString() to ensure that the outputstring always are using '.' as separator without thousand sep, and what parameter shall I use to convert double to Double.Parse() or...
0
9587
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, 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...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, 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...
1
9993
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
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...
1
7406
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 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...
0
6672
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();...
0
5298
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...
3
2815
bsmnconsultancy
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...

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.