1. ## decimal to fraction

Hi,

Is it possible to change a decimal (it will always be less than 1 and more than 0) to a fraction in its most simplified form?

Eg. var dec='0.75'

The script would make var frac='3/4'

Thanks,

Neil

Code:
```<script type="text/javascript">
Number.prototype.fraction=fraction;
function fraction(decimal){
if(!decimal){
decimal=this;
}
whole = String(decimal).split('.')[0];
decimal = parseFloat("."+String(decimal).split('.')[1]);
num = "1";
for(z=0; z<String(decimal).length-2; z++){
num += "0";
}
decimal = decimal*num;
num = parseInt(num);
for(z=2; z<decimal+1; z++){
if(decimal%z==0 && num%z==0){
decimal = decimal/z;
num = num/z;
z=2;
}
}
return ((whole==0)?"" : whole+" ")+decimal+"/"+num;
}
</script>```
You can run it either of these two ways:

x=.75;

or

3. That will needlessly clutter the name space. Use instead:
Code:
```<script type="text/javascript">
Number.prototype.fraction = function (decimal){
if(!decimal){
decimal=this;
}
var whole = String(decimal).split('.')[0];
decimal = parseFloat("."+String(decimal).split('.')[1]);
var num = "1";
for(var z=0; z<String(decimal).length-2; z++){
num += "0";
}
decimal = decimal*num;
num = parseInt(num);
for(var z=2; z<decimal+1; z++){
if(decimal%z==0 && num%z==0){
decimal = decimal/z;
num = num/z;
z=2;
}
}
return ((whole==0)?"" : whole+" ")+decimal+"/"+num;
}
</script>```

4. That is how I had it written in the first place, but I wanted to make it so it could be called like this:

Which can't be done with what you posted... Is there a different way of doing that besides what I did?

5. All I did was clean up your function using a function literal, to avoid the unnecessary use of the "function" name, and I made the variables you are using in the function local.

6. Originally posted by Charles
All I did was clean up your function using a function literal, to avoid the unnecessary use of the "function" name, and I made the variables you are using in the function local.
...and you made it so it doesn't work like this:

which was my whole point in doing it that way in the first place. Thanks for fixing my code...

...and you made it so it doesn't work like this:

which was my whole point in doing it that way in the first place. Thanks for fixing my code...
Fair enough, but the 'var' I put in every now and again is rather important.

8. Hi,

I've now got the following script:

Code:
```<html>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
<script type="text/javascript">
Number.prototype.fraction=fraction;
function fraction(){
decimal=document.getElementById('dec').value
if(!decimal){
decimal=this;
}
whole = String(decimal).split('.')[0];
decimal = parseFloat("."+String(decimal).split('.')[1]);
num = "1";
for(z=0; z<String(decimal).length-2; z++){
num += "0";
}
decimal = decimal*num;
num = parseInt(num);
for(z=2; z<decimal+1; z++){
if(decimal%z==0 && num%z==0){
decimal = decimal/z;
num = num/z;
z=2;
}
}
return ((whole==0)?"" : whole+" ")+decimal+"/"+num;
}
</script>

<body>
<input type="text" name="T1" size="20" id='dec'>
</body>

</html>```
With this, you enter a decimal into the textbox and click a button, and the fraction pops up. It works for most factions, but for some reason it doesn't work with 0.55

Any ideas why 0.55 doesn't work?

Thanks,

Neil

9. Code:
```<html>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
<script type="text/javascript">
Number.prototype.fraction=fraction;
function fraction(){
decimal=document.getElementById('dec').value
if(!decimal){
decimal=this;
}
whole = String(decimal).split('.')[0];
decimal = parseFloat("."+String(decimal).split('.')[1]);
num = "1";
for(z=0; z<String(decimal).length-2; z++){
num += "0";
}
decimal = parseInt(decimal*num);
num = parseInt(num);
for(z=2; z<decimal+1; z++){
if(decimal%z==0 && num%z==0){
decimal = decimal/z;
num = num/z;
z=2;
}
}
return ((whole==0)?"" : whole+" ")+decimal+"/"+num;
}
</script>

<body>
<input type="text" name="T1" size="20" id='dec'>
</body>
</html>```
For some reason, the math in JavaScript is not always correct... I don't know why, but it sure is a pain. This should work fine now, though...

10. Thanks, I can't find any prob's now, the code works perfectly.

Thanks,

Neil

11. No problem. I'm glad to help.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.40704 seconds
• Memory Usage 2,953KB
• Queries Executed 13 (?)
Template Usage (34):
• (4)bbcode_code
• (2)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (11)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (11)postbit
• (11)postbit_onlinestatus
• (11)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete