Python Forum
How show decimal only for none whole number ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How show decimal only for none whole number ?
#1
Hi everyone,

How show decimal only for none whole number ?

and when the number have decimal limit their number to two.


I've tried this :
test = 65654848 
print(f'{test:,.2f})
Output:
65,654,848.00
not what i'm looking for Undecided
[Image: NfRQr9R.jpg]
Reply
#2
I don't understand the question. What output do you want?
Reply
#3
Thanks @ndc85430

Actually I've found a solution, not the most elegant but it work

def F_minimalNumber(x):
    f = float(x)
    if f.is_integer():
        return (f'{int(f):,}'.replace('.','_').replace(',','.').replace('_',','))
    else:
        return (f'{round(f,2):,}'.replace('.','_').replace(',','.').replace('_',','))

print(F_minimalNumber(65654848.6546843))
print(F_minimalNumber(65654848))
So it's round the number when have decimal and show maximum 2 decimal.
and show no decimal ex:(.00) when no decimal
and change the , for . for the thousand separator.
Output:
65.654.848,65 65.654.848
[Image: NfRQr9R.jpg]
Reply
#4
Simple way

#! /usr/bin/python3.8

num = 123
num2 = 123.5012569
num3 = 123.05


def numbers(num):
  if isinstance(num, float):
    print('%.2f' % round(num, 2))
  else:
    print(num)


numbers(num)
numbers(num2)
numbers(num3)
Output:
123 123.50 123.05

Just wanted to add one

#! /usr/bin/python3.8

num = 123
num2 = 123.5012569
num3 = 123.05
num4 = 125.00

def numbers(num):
  if num == int(num):
    print(int(num))

  elif isinstance(num, float):
    print('%.2f' % round(num, 2))
  else:
    print(num)


numbers(num)
numbers(num2)
numbers(num3)
numbers(num4)
Output:
123 123.50 123.05 125
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
Sorry @menator01 but your output is
Output:
123.50
but I was needed 123,50 (coma for decimal separator)

https://en.wikipedia.org/wiki/Decimal_se...arator.svg
[Image: NfRQr9R.jpg]
Reply
#6
This is only for sake of humans to improve readability. Python can't perform calculations with values formatted this way (except string concatenation).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
import locale


lang, encoding = locale.getlocale()
locale.setlocale(locale.LC_ALL, lang)


x = 1337.42
print(f"{x:n}")
What is n?
This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters.

https://docs.python.org/3/library/string...matstrings
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PIL Image im.show() no show! Pedroski55 2 958 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  PIL Image im.show() no show! Pedroski55 6 4,879 Feb-08-2022, 06:32 AM
Last Post: Pedroski55
  Calculate the Euler Number with more decimal places Pedroski55 10 4,495 Oct-31-2021, 04:45 AM
Last Post: Pedroski55
  Creating table in MySQL db with decimal number issue dangermaus33 7 4,876 Nov-20-2020, 10:40 PM
Last Post: dangermaus33
  Numpy savetxt, how save number with decimal separator SpongeB0B 1 3,208 May-10-2020, 01:05 PM
Last Post: ThomasL
  testing for Decimal w/o importing decimal every time Skaperen 7 4,432 May-06-2019, 10:23 PM
Last Post: Skaperen
  If a decimal is 0 how do I make it not show? GamingDay 4 2,664 Feb-08-2019, 01:00 AM
Last Post: GamingDay

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020