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

Friday, August 12, 2022

[FIXED] How to get decimal parts in integer from double?

 August 12, 2022     decimal, decimal-point, java, math     No comments   

Issue

How to separate double into two integer number? First number before and second number after decimal point. For example:

            double doub = 543.345671;
            int num1 = (int) doub; //543
            int num2 = getNumAfterDecimal(doub-num1); //return 345671

I need decimal part to integer.


Solution

It depends how many digits you want after the decimal point, but this is the gist:

double d = doub - (int)doub; // will give you 0.xyz
int result = d * 1000; // this is for 3 digits, the number of zeros in the multiplier decides the number of digits 


Answered By - Nir Levy
Answer Checked By - Terry (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

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