# convert fraction into string and also insert [] for repeating part

Error processing SSI file

1. #### Colombo

• 2019/7/20

For fixed point, simply multiply the numerator with 10 before dividing. For floating point, simply use division then `modf` to get the fractional part. In either case, convert to string then format as preferred (1 decimal).

In C you could do something generic that handles either fixed or floating point, by use of `_Generic`.

A simple example without any error handling:

```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

char* strfract_int (char* dst, int n, int d)
{
sprintf(dst, "0.%d", n*10 / d);
return dst;
}

char* strfract_double (char* dst, double n, double d)
{
double dummy;
sprintf(dst, "%.1f", modf(n/d, &dummy) );
return dst;
}

#define strfract(dst, n, d)                   \
_Generic((n),                               \
int:    strfract_int,              \
double: strfract_double)(dst,n,d)  \

int main (void)
{
char buf [100];
puts( strfract(buf, 1, 3) );
puts( strfract(buf, 2, 5) );
puts( strfract(buf, 1.0, 3.0) );
puts( strfract(buf, 2.0, 5.0) );
}
```

In a rugged program, check for division against zero, the result of malloc etc etc.

2. #### Vicente

• 2018/2/13

Given two int N (numerator) and D (denominator), return the fraction in string. if the fraction is repeating, then display the repeating part in

3. #### Theo

• 2021/7/21

convert fraction into string and also insert [] for repeating part return the fraction in string. if the fraction is repeating, then display the repeating part in

4. #### Hendrix

• 2017/8/2

To check if the fractional part is repeating, insert the remainder (numerator % denominator) in a map with key as remainder and value as the

5. #### Green

• 2016/5/11

I would avoid using a double like the plague. Due to finite precision, it will not give you the correct answer. Stick to integer arithmetic and simulate long division, keeping track of the remainder. After you run out of digits in the numerator (so you are bringing down zeros), also keep a history of remainders and if you see a remainder that is already in the history, that tells you that you have hit a repeating sequence. You can then construct the bracketed output part. (If you hit a zero remainder, of course, that means that the answer is a terminating fraction.)

6. #### Brett

• 2021/9/9

convert fraction into string and also insert [] for repeating part An interview question: Given two int N (numerator) and D (denominator), return the fraction in string. if the fraction is repeating, then display the repeating part in bracket.

• 2015/5/3

Let us simulate the process of converting fractions to decimals. Let us look at the part where we have already figured out the integer part,

8. #### Jesus

• 2017/10/25

Represent the fraction of two numbers in the string format. Given two integers representing the Numerator and Denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.

9. #### Tru

• 2019/7/29

Here is a rust solution which simulates long division for the fractional part.

There will only be a repeating decimal if the divisor has a factor other than 2 and 5. We can easily check for this by repeatedly dividing by 2 until all factors of 2 are removed and then repeatedly dividing by 5 until all factors of 5 are removed. After dividing, if the resultant number is 1, then there will be no repeating decimal.

To handle the case for the repeating decimal, we keep a hashset of the remainders we have seen until now. If a remainder reappears, we stop the computation.

```#[allow(unused_variables)]

use std::collections::HashSet;
use std::io;

fn main() {
println!("{}", long_division(a, b));
}

let mut input_string = String::new();
io::stdin()
let trimmed = input_string.trim();
let num = trimmed.parse::<usize>().unwrap();
num
}

fn repeating_decimal(denominator: usize) -> bool {
let mut d = denominator;
while d % 2 == 0 {
d /= 2;
}
while d % 5 == 0 {
d /= 5;
}
if d == 1 { false } else { true }
}

fn long_division(a: usize, b: usize) -> String {
let q = a / b;
let r = (a % b) * 10;
if r == 0 {
return q.to_string();
}
if repeating_decimal(b) {
format!("{}.({})",q,divide_repeating_decimal(r, b))
} else {
format!("{}.{}",q,divide_without_repeating_decimal(r, b))
}

}

fn divide_repeating_decimal(mut r: usize, b: usize) -> String {
let mut result = String::new();
let mut seen: HashSet<usize> = HashSet::new();
loop {
if r < b {
r *= 10;
result.push_str(&"0".to_owned());
continue;
}
let q = r / b;
r = (r % b) * 10;
result.push_str(&q.to_string());
if seen.contains(&r) {
break;
}
seen.insert(r.clone());

}
result
}

fn divide_without_repeating_decimal(mut r: usize, b: usize) -> String {
let mut result = String::new();
while r != 0 {
if r < b {
r *= 10;
result.push_str(&"0".to_owned());
continue;
}
let q = r / b;
r = (r % b) * 10;
result.push_str(&q.to_string());
}
result
}
```
10. #### Ridge

• 2019/5/5

If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, return "0.5".

11. #### Richard

• 2016/4/1

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return any of them .

12. #### Keanu

• 2017/4/19

To check if the fractional part is repeating, insert the remainder (numerator % denominator) in a map with key as remainder and value as the

13. #### Enrique

• 2019/12/2

You can use this repeating decimal to fraction conversion calculator to revert a repeating decimal to its original fraction form. Simply input the repeating part of the decimal (the repetend) and its non-repeating part (where applicable). For instance, if you are converting 0. 6 to 2/3, leave the non-repeating field blank.

14. #### Jairo

• 2017/8/29

The easiest of the conversion cases will also be the building block of all the rest: converting a string to a double. In a new playground, add

15. #### Aron

• 2018/2/4

This video explains how to convert a repeating decimal to a fraction.http://mathispower4u.com

16. #### Harlan

• 2016/6/9

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating,

17. #### Maximilian

• 2017/1/21

exception '-' * This method expects an integer as a String and calculates its fraction to binary is kind of the opposite else procedure of converting a

18. #### Rafael

• 2017/2/26

If the repeating part is simply a string of 0s so that the decimal is actually Solution To change (lg to a fraction, enter If x I 0.232323 - - -

19. #### Sawyer

• 2019/10/23

In this case, the repeated string is consists of one digit (5). To convert into a fraction, divide the repeated string by 9. If the repeated string is two