Ask Your Question

# How to output 'decimal' numbers in different radix.

For example, how to output the fraction 13/7 as a 'decimal' number in the base 6? Perhaps to 4 'digits' after the 'decimal' point if the representation is not finite.

edit retag close merge delete

## 2 Answers

Sort by ยป oldest newest most voted

You can use the str method:

sage: (13/7)
13/7
sage: (13/7).str(base=6)
'21/11'
sage: (13/7).n()
1.85714285714286
sage: (13/7).n().str(base=6)
'1.505050505050505050511'

more

## Comments

Thanks for the solution!

( 2012-12-29 01:23:16 +0200 )edit

The following function converts integers in any base to any other base:

def base_conversion(number, base_from, base_to):
if(base_from==10):
return (number.str(base=base_to))
else:
decimal = Integer(str(number), base=base_from)
return (decimal.str(base=base_to))

more

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2012-12-29 00:09:31 +0200

Seen: 1,133 times

Last updated: Dec 29 '12