Python Forum
formatting float like int for whole values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
formatting float like int for whole values
#1
i know i have seen this before but now that i have a use case, i can't find it.
Output:
>>> foo(3.25) '3.25' >>> foo(4.0) '4' >>>
i want to find what works like foo() in this fake example.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Check https://stackoverflow.com/q/2440692/4046632
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
i thought i had seen it as a single function. i want to do this in an f-string. i want to keep it simple. maybe it was something i had coded a long time ago like answer #3 in that link.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Something like this perhaps.
def foo (floating_point_number) :
	integer = int (floating_point_number)
	if integer == floating_point_number :
		return integer
	return floating_point_number

print (f'{foo (3.25)}, {foo (4.0)}')
Reply
#5
close to that. while str() isn't exactly necessary, that was what i had in mind ... that foo() did the formatting.
def foo (floating_point_number) :
    integer = int (floating_point_number)
    if integer == floating_point_number :
        return str(integer)
    return str(floating_point_number)

print (f'{foo (3.25)}, {foo (4.0)}')
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Do you mean like this?
def foo (floating_point_number) :
	integer = int (floating_point_number)
	if integer == floating_point_number :
		print (integer)
	else :
		print (floating_point_number)

foo (3.25)
foo (4.0) 
Reply
#7
not to print in the function since i may want 2 or more on the same line. i may or may not need a str returned.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
You said that you want this:
Output:
>>> foo(3.25) '3.25' >>> foo(4.0) '4' >>>
and I gave you this:
Output:
>>> def foo (floating_point_number) : ... integer = int (floating_point_number) ... if integer == floating_point_number : ... return integer ... return floating_point_number ... >>> foo (3.25) 3.25 >>> foo (4.0) 4 >>>
The only difference I see is the quotation marks. Is that what your looking for? If not, can you explain exactly what the difference is?
Reply
#9
The difference is return a string, not a number. I think Skaperen wants something that returns the shortest string that accurately represents the value of a number.
Skaperen likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 315 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  Formatting float number output barryjo 2 934 May-04-2023, 02:04 PM
Last Post: barryjo
  ValueError - Formatting issue when values enter thousands phillyfa 4 1,194 Apr-20-2023, 06:22 PM
Last Post: phillyfa
  Write Null values as 0.0 (float) type in csv mg24 3 1,382 Dec-07-2022, 09:04 PM
Last Post: deanhystad
  Float Slider - Affecting Values in Column 'Pandas' planckepoch86 0 1,405 Jan-22-2022, 02:18 PM
Last Post: planckepoch86
  Float Formatting Kristenl2784 3 1,757 Jul-30-2020, 04:49 PM
Last Post: bowlofred
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,399 Jul-25-2019, 11:32 PM
Last Post: scidam
  Unable to print the exact Float values when I convert LIST to Sequence of Tuples? preethamalluri 1 2,441 Jul-12-2018, 09:03 AM
Last Post: buran
  Can PyAudio (Port Audio) validly accept float values when writing to stream? cdrandin 1 3,789 Mar-26-2017, 07:54 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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