PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] how to pick up to 2 or 3 decimals with lists using python2?

 August 11, 2022     decimal, list, python-2.7     No comments   

Issue

I am reading data from the robot's sensor. it gives this data as a list:

data:[1.0014142543, 0.4142543254, 4.5432544179]

I'd like to convert it like this format:

data:[1.00, 0.41, 4.54]

I am running it with python2.

I think this post for one number.

any ideas or suggestions, it would be highly appreciated.


Solution

Considering data as list ,

Using round :

>> data=[1.00, 0.41, 4.54]
>> [round(item,2) for item in data]

Using Float and Format:

>> [float(format(item,".2f")) for item in data]

Output :

[1.0, 0.41, 4.54]


Answered By - redhatvicky
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,210,817

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing