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

Tuesday, August 9, 2022

[FIXED] How to display 3 decimal points without round up?

 August 09, 2022     android, bigdecimal, decimal, java, number-formatting     No comments   

Issue

I am making an app which need to display 3 decimal points i achieve this by NumberDecimal but the issue is if 4th digit is >5 then 3rd digit automatically increase. I want to show exact figure for ex :

Result           : 100.2356 
Output           : 100.236  // Applying NumberDecimal

Need this output : 100.235 // I need this output

This is my function :
public String setdecimal(float a) 
{
   NumberFormat formatter = new DecimalFormat("##.###");
   return formatter.format(a);

}

Solution

Use RoundingMode.FLOOR:

NumberFormat formatter = new DecimalFormat("##.###");
formatter.setRoundingMode(RoundingMode.FLOOR);

Depending on how you want to handle negative numbers, you could also use RoundingMode.DOWN, which rounds towards zero.



Answered By - samgak
Answer Checked By - Cary Denson (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