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

Wednesday, August 10, 2022

[FIXED] How can limit a certain number of decimal places being shown without rounding?

 August 10, 2022     decimal, python, rounding     No comments   

Issue

I would like to be able to limit the amount of decimal places that are shown from a print function without any sort of rounding system

v = 8.836333333333339

print ('%.2f' % v)

This code will print the value of v to two decimal places but also rounds it up or down, how could I make it stop this rounding please?


Solution

You could process it as a string:

v = 8.836333333333339
s = str(v)
print s[:s.find('.')+3]

# prints 8.83


Answered By - ooga
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • 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

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 © PHPFixing