473,772 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert between bases with decimals

Hi, does anybody know how to convert in C++ from base 10 to any other
base without loosing the decimal part of the actual value?

I came up with this algorithm to convert from decimal to any base but
I'm not sure what to do to include the decimal part.

//-----------------------------------------------------------
const char ccBaseChars[] = "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ";
char sResult [80] = {0};
int iBase;
int iRemainder;

long double dValue = 14.3;

while (dValue > 0)
{
iRemainder = std::floorl(std ::fmodl(dValue , iBase), 0);
Value = std::floorl(dVa lue / iBase);

if (iRemainder == iBase)
sResult = sResult + ccBaseChars [0];
else
sResult = sResult + ccBaseChars [iRemainder];
}
//-----------------------------------------------------------

<>Thanks for any help
Jul 22 '05 #1
1 2576
"ferran" <fe*****@yahoo. com> wrote...
Hi, does anybody know how to convert in C++ from base 10 to any other
base without loosing the decimal part of the actual value?
Fractions can be converted. However, you have to take into consideration
the fact that while the integral part can be converted precisely in
a limited number of digits, the fractional part often cannot, and does
require infinite number of digits in the other base to represent a finite
number of digits in the original one. Example: "one and one third" if
written in _ternary_ system, would look like "1.1". However, in decimal
the same number is 1.3333333333333 333333333333333 333333333333333 33333...
Another pair: Decimal 1.1 in binary will be 1.0001100110011 001100110011...
I came up with this algorithm to convert from decimal to any base but
I'm not sure what to do to include the decimal part.
Fractions are sort of simple: continuously multiply the number (the
fraction) by the base. If you get an integral part, write down that digit,
then subtract it. If you get exactly 0, stop. If you don't, keep going.
Sometimes, like when converting decimal 0.1 into binary, you _never_ get
precisly 0. That's when you get the _infinite_ number of digits in the
fractional part.

//-----------------------------------------------------------
const char ccBaseChars[] = "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ";
char sResult [80] = {0};
int iBase;
int iRemainder;

long double dValue = 14.3;

while (dValue > 0)
{
iRemainder = std::floorl(std ::fmodl(dValue , iBase), 0);
Value = std::floorl(dVa lue / iBase);

if (iRemainder == iBase)
sResult = sResult + ccBaseChars [0];
else
sResult = sResult + ccBaseChars [iRemainder];
}
//-----------------------------------------------------------

<>Thanks for any help

Jul 22 '05 #2

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

Similar topics

4
20150
by: Dean G | last post by:
I need to compare two values. one from a text field 'bid' and the other from a field in an sql server database 'maxbid'. The problem is the column in the database has decimal as its data type and i'm getting a type mismatch. does anyone know how to convert 'bid' into decimal from varchar? the field datatype doesnt necessarily have to be decimal although i need two decimal places so it cant be an int. Thanks, Dean
5
8260
by: da Vinci | last post by:
Hi Gents, This is what I am trying to do. Say you have a double or a float with the value 14.5624 for example. How could I take that variable and get the 14 into an integer variable and the .5624 into a double or float? The reason is because of converting decimal locations into DMS. Math is simple....
13
41818
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
3
14522
by: Chua Wen Ching | last post by:
I have a problem. But on .NET 1.1 My Scenario: Actually I will have a string of hexadecimals read from a xml file. Then from the hexadecimals, i will add 1 value whenever i made any modifications. But just i do not how to get started. It is like an incremental hexadecimals. I have a string of hex. string strHex = "0100000000FF";
5
27686
by: 2redline | last post by:
I am trying to write a query in access that will pull results that are in the database in a text field and convert to where I can get a average including decimals. Any Ideas? -----------------------Start SQL-------------------------------------- SELECT AVG(CAST(dbo_sur_response_answer.answer_text AS int)) AS avg_correct FROM (dbo_sur_response_answer INNER JOIN dbo_sur_subitem
4
4523
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However it seems this convert is determined by the local settings and comma is indeed used as decimal separator. Is there another way to convert a dotted value to a double variable? Like 1234.5 and not 1234,5
52
1565
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly.
1
2921
by: Evando | last post by:
I need a possible solution to tackle my assignment.The instruction again is to write a program code in C to convert from Roman numbers into Decimals.Any possible solution which will run is needed. THANKS.
6
16919
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
0
10264
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...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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
9914
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...
0
8937
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, 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...
0
5355
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...
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
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.