Python convert int to string without decimal

    1. Public
    2. Questions
    3. Collectives

    4. Explore Collectives
    1. Teams

      Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

      Python convert int to string without decimal
      Create a free Team Why Teams?

    2. Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Asked 8 years, 4 months ago

Viewed 16k times

If I have f=123.12, what is easier way to change to string s="123"? For now I do: s = "%.0f" % (f,).

  • python

asked May 20, 2014 at 5:31

user3654650user3654650

4,65510 gold badges26 silver badges28 bronze badges

4

  • Should it be Rounded or truncated?

    May 20, 2014 at 5:32

  • sshashank124 anwser is good, but cant accept yet. thank you.

    May 20, 2014 at 5:35

  • No problem. Glad I could help

    May 20, 2014 at 5:35

  • @user3654650 If any answer solves your problem then please don't forget to accept it. It has many advantages: The person who invested his/her time for answering gets reputation points, YOU also get some reputation points, finally this allows others users to know that this question is solved.

    May 20, 2014 at 8:06

1 Answer

If you want to round the value, do:

str(int(f+0.5))

If you want to truncate the value, do:

str(int(f))

answered May 20, 2014 at 5:32

sshashank124sshashank124

30.3k8 gold badges63 silver badges75 bronze badges

0

  • The Overflow Blog
  • Featured on Meta

Related

Hot Network Questions

  • If a bard has two instruments can they cast regularly still?
  • How are HiFi DACs constructed?
  • Do I have to report all data and how should I deal with outliers if there are too many?
  • Probability of winning the series given not playing the first game
  • What is that blue round thing below the resistor?
  • Can a decision to prosecute be judicially appealed?
  • Why does math need to be practiced and exercised, when L1 Linguistic Competence is subconscious?
  • From a safety standpoint, which is better: voice aural alerts or tone aural alerts?
  • Anyone up for decompiling some 8080 code for Kaleidoscope?
  • the correct way to deal with spaces in a pattern for grep
  • Work Integral and its derivation
  • Membrane protein diffusion in different confinement models
  • Is this an old, alternate or incorrect way of writing 考?
  • Use asterisk-like command in normal mode to execute a find/replace
  • Torus shaped event horizon
  • What is the origin of the "self-destruct sequence"
  • i'm still confused about "for me" vs "to me"
  • Travel with luggage from SAW Airport Istanbul to Europe Side of Istanbul via Metro or Bus
  • Can someone identify these Lego sets? White with large transparent blue walls
  • How can a species be fine with digesting poisons and toxins?
  • Yerushalmi Horoyos in Shas Vilna
  • List of countries embargoed by the US?
  • What's the first anime adapted from a galgame?
  • What is the role of this contraption that came with the shower hose?

more hot questions

Question feed

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

  • Problem Formulation
  • Solution 1: int()
  • Solution 2: round()
  • Solution 3: Print Decimal and Non-Decimal Parts Separately
  • Where to Go From Here?

Problem Formulation

  • Given a floating point number x = 123.456.
  • How to print the number without decimal remainder?
x = 123.456
print(x)
# Desired output: 123
# Real output: 123.456

Solution 1: int()

Python’s built-in function int(x) converts any float number x to an integer by truncating it towards 0. For example, positive int(1.999) will be truncated to 1 and negative int(-1.999) to -1.

>>> int(123.456)
123
>>> int(-123.456)
-123

To boost your skills, feel free to watch my explainer video on the int() function next:

Python int() Function

Solution 2: round()

Python’s built-in function round(x), when called with a single float argument x, converts the float to an integer by rounding it up and down to the next integer. For example, int(1.999) will be rounded to 2 and int(-1.999) to -2.

>>> round(123.456)
123
>>> round(-123.456)
-123

To boost your skills, feel free to watch my explainer video on the round() function next:

Python round() — A Helpful Interactive Guide

Solution 3: Print Decimal and Non-Decimal Parts Separately

Given a string representing a floating point number, you can print the parts before and after the decimal point by splitting the string on the decimal point and converting the two parts to integers by using the int() built-in function in a list comprehension statement.

x = '123.456'
num, decimal = [int(part) for part in x.split('.')]

print(num)
# 123

print(decimal)
# 456

This snippet builds on four Python concepts you need to understand first:

  • int() function,
  • multiple assignment,
  • list comprehension, and
  • string.split() method.

Let’s explore the most important one, the list comprehension statement, in the following video!

A Simple Introduction to List Comprehension in Python

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Python convert int to string without decimal

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

How do you return no decimal value in Python?

Use the int() class to get a number without the decimal places, e.g. result = int(my_float) . The int() class truncates floating-point numbers towards zero, so it will return an int that represents the number without the decimal places.

How do you print without decimal in Python?

Here we have to work on the float numbers, so let's list out the different methods of removing decimals from numbers..
a = 24..
print(type(a)).
b = 19.4..
print(type(b)).
c = 3+4j..
print(type(c)).

How do you format no decimals in Python?

Solution 1: int() Python's built-in function int(x) converts any float number x to an integer by truncating it towards 0. For example, positive int(1.999) will be truncated to 1 and negative int(-1.999) to -1 .

How do you convert an int to a string in Python?

To convert an integer to string in Python, use the str() function. This function takes any data type and converts it into a string, including integers. Use the syntax print(str(INT)) to return the int as a str , or string.