Lyux
Lyux

Reputation: 463

Convert Cents to Euro

i'm relatively new to Phyton programming so excuse me if this question seems dumb but i just cant find an Answer for it.

How do i convert a number like lets say 1337 to 13,37€

All I have for now is this Code Sample:

number = 1337

def price():
    Euro = 0
    if number >= 100
        Euro = Euro + 1

But obviously i need to put this in some kind of for-loop, kinda like this:

number = 1337

def price():
    Euro = 0
    for 100 in number:
        Euro = Euro + 1

But this doesn't seem to work for me.

EDIT:

Also, how do i convert that number correctly if i just divide by 100 i get 13.37 but what i want is 13,37€.

Upvotes: 0

Views: 5539

Answers (2)

elegent
elegent

Reputation: 4007

Because Python does not support any currency symbols in numbers, you need to convert a number to a string and append the currency ("€") character to it: str(13.37) + "€":

def convertToCent(number):
    return '{:,.2f}€'.format(number/100)

As @ozgur and @chepner pointed out the so called "true division" of two integers is only possible in Python 3:

x / y returns a reasonable approximation of the mathematical result of the division ("true division")

Python 2 only returns the actual floating-point quotient when one of the operands are floats.

The future division statement, spelled "from __future__ import division", will change the / operator to mean true division throughout the module.

For more information see PEP 238.

Upvotes: 3

PanGalactic
PanGalactic

Reputation: 72

This shouldn't be hard to guess:

number = 1337

def price(number):
    return (euro / 100.0)

print prince(number)

Upvotes: -2

Related Questions