Last active
April 19, 2016 14:09
-
-
Save ugurunver/f974ff7ed02e2ddbbb28 to your computer and use it in GitHub Desktop.
sayıyı yazıya dönüştür
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from decimal import Decimal | |
sayilar = { | |
"0" : "", | |
"1" : "bir", | |
"2" : "iki", | |
"3" : "uc", | |
"4" : "dort", | |
"5" : "bes", | |
"6" : "alti", | |
"7" : "yedi", | |
"8" : "sekiz", | |
"9" : "dokuz" | |
} | |
onlar = { | |
"0" : "", | |
"1" : "on", | |
"2" : "yirmi", | |
"3" : "otuz", | |
"4" : "kirk", | |
"5" : "elli", | |
"6" : "altmis", | |
"7" : "yetmis", | |
"8" : "seksen", | |
"9" : "doksan" | |
} | |
yuzler = { | |
"0" : "", | |
"1" : "yuz", | |
} | |
map(lambda x:yuzler.update({x:sayilar[x]+"yuz"}), filter(lambda n:int(n)>1,sayilar) ) | |
#yuzler.update({k:sayilar[k]+"yuz" for k, v in map(lambda t:(t,sayilar[t]),filter(lambda x:int(x)>1, sayilar.keys())) }) | |
adres = [sayilar, onlar, yuzler] | |
basamak_eki = ["", "bin", "milyon","milyar","trilyon","katrilyon"] | |
class Yazi: | |
def __init__(self,sayi, ondalik_sayi=2, tam_birim="lira",ondalik_birim="kurus"): | |
self.ondalik_sayi = ondalik_sayi | |
tam, ondalik = self.ondalik_parcala(sayi) | |
self.tam_yazi = self.yaz(tam) | |
self.ondalik_yazi = self.yaz(ondalik) | |
sonuc = [] | |
if self.tam_yazi: | |
sonuc+=[self.tam_yazi, tam_birim] | |
if self.ondalik_yazi: | |
sonuc+=[self.ondalik_yazi, ondalik_birim] | |
self.sonuc = "".join(sonuc) | |
def __str__(self): | |
return self.sonuc | |
def __unicode__(self): | |
return self.sonuc | |
def __repr__(self): | |
return self.sonuc | |
def ondalik_parcala(self,sayi): | |
sayi = str(sayi).replace(",",".") | |
splitted = sayi.split(".") | |
if len(splitted) == 1: | |
tam=sayi | |
ondalik = "" | |
elif len(splitted) == 2: | |
tam = splitted[0] or "0" | |
ondalik = splitted[1] | |
else: | |
tam = "".join(splitted[:-1]) or "0" | |
ondalik = splitted[-1] | |
while len(ondalik) != self.ondalik_sayi and ondalik!="" and ondalik!="0": | |
if len(ondalik) < self.ondalik_sayi: | |
ondalik = str(int(ondalik)*10) | |
else: | |
_ondalik = str(round(Decimal("0."+str(ondalik)),self.ondalik_sayi)).split(".") | |
ondalik = _ondalik[1] | |
tam = str(int(tam)+int(_ondalik[0])) | |
return tam,ondalik | |
def ters_cevir(self, x): | |
tersi = "" | |
for rakam in x: | |
tersi = rakam+tersi | |
return tersi | |
def blok_yaz(self, parca): | |
yazi = "" | |
for ii, rakam in enumerate(parca): | |
yazi = adres[ii][rakam] +yazi | |
return yazi | |
def yaz(self, rakamlar): | |
yazi = "" | |
rakamlar = self.ters_cevir(rakamlar) | |
parcalar = [rakamlar[i:i+3] for i in range(0, len(rakamlar), 3)] | |
for i,parca in enumerate(parcalar): | |
if i==1 and str(parca)=="1": | |
_blok_degeri = "" | |
else: | |
_blok_degeri = self.blok_yaz(parca) | |
yazi = _blok_degeri+basamak_eki[i] + yazi | |
return yazi | |
if __name__ == "__main__": | |
print ".", Yazi(".") | |
print 12345.2343, Yazi(12345.2343) | |
print Decimal(".34326"), Yazi(Decimal(".34326")) | |
print Decimal(".9999"), Yazi(Decimal(".9999")) | |
print 99999, Yazi(99999) | |
print 123.343, Yazi(123.343,3) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment