How to get numbers after decimal point?
An easy approach for you:
number_dec = str(number-int(number))[1:]
Get the number of digits after the decimal point of a float (with or without decimal part)
I found some simple script (relatively to me) to handle this.
ISNULL(NULLIF(CHARINDEX('.',REVERSE(CONVERT(VARCHAR(50), Amount, 128))),0) - 1,0)
Here the ISNULL(NULLIF
is only to handle the float without decimal part.
If there is no values without decimal part, then it is very simple
CHARINDEX('.',REVERSE(CONVERT(VARCHAR(50), Amount, 128))) -1
Hope this will be helpful to you.
Full script below
declare @YourTable table (Amount float)
insert into @YourTable
values(123),(123.1),(123.0123),(123.789456)
SELECT ISNULL(NULLIF(CHARINDEX('.',REVERSE(CONVERT(VARCHAR(50), Amount, 128))),0) - 1,0)
FROM @YourTable
SELECT CHARINDEX('.',REVERSE(CONVERT(VARCHAR(50), Amount, 128))) -1
FROM @YourTable
Get decimal portion of a number with JavaScript
Use 1
, not 2
.
js> 2.3 % 1
0.2999999999999998
PHP: get number of decimal digits
$str = "1.23444";
print strlen(substr(strrchr($str, "."), 1));
Finding the number of places after the decimal point of a Double
There's no easy way, especially since the number of digits mathematically speaking might be far more than displayed. For example, 4.5565 is actually stored as 4.556499999999999772626324556767940521240234375
(thanks to harold for calculating that). You're very unlikely to find a useful solution to this problem.
EDIT
You could come up with some algorithm that works like this: if, as you calculate the decimal representation, you find a certain number of 9s (or zeros) in succession, you round up (or down) to the last place before the series of 9s (or zeros) began. I suspect that you would find more trouble down that road than you would anticipate.
Getting the decimal part of a double in Swift
Without converting it to a string, you can round up to a number of decimal places like this:
let x:Double = 1234.5678
let numberOfPlaces:Double = 4.0
let powerOfTen:Double = pow(10.0, numberOfPlaces)
let targetedDecimalPlaces:Double = round((x % 1.0) * powerOfTen) / powerOfTen
Your output would be
0.5678
How to get whole and decimal part of a number?
$n = 1.25;
$whole = floor($n); // 1
$fraction = $n - $whole; // .25
Then compare against 1/4, 1/2, 3/4, etc.
In cases of negative numbers, use this:
function NumberBreakdown($number, $returnUnsigned = false)
{
$negative = 1;
if ($number < 0)
{
$negative = -1;
$number *= -1;
}
if ($returnUnsigned){
return array(
floor($number),
($number - floor($number))
);
}
return array(
floor($number) * $negative,
($number - floor($number)) * $negative
);
}
The $returnUnsigned
stops it from making -1.25 in to -1 & -0.25
Truncate number to two decimal places without rounding
Convert the number into a string, match the number up to the second decimal place:
function calc(theform) { var num = theform.original.value, rounded = theform.rounded var with2Decimals = num.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0] rounded.value = with2Decimals}
<form onsubmit="return calc(this)">Original number: <input name="original" type="text" onkeyup="calc(form)" onchange="calc(form)" /><br />"Rounded" number: <input name="rounded" type="text" placeholder="readonly" readonly></form>
Related Topics
Better Techniques for Trimming Leading Zeros in SQL Server
Number of Times a Value Appears (Sql)
Database Corruption With Mariadb:Table Doesn't Exist in Engine
Mysql - Get All Records That Have More Than 1 Record for the Same Id
Combine Multiple Columns from Database into One Column
Mysql - Operand Should Contain 1 Column(S)
Mysql - How to Use Like on Multiple Columns
Solve Query for Showing Top 5 Selling Products
Hive Select Data into an Array of Structs
How to Include Results of SQL Count If Count=0
Mismatched Input 'From' Expecting <Eof> SQL
Select Distinct Records on a Join
How to Combine First Name, Middle Name and Last Name in SQL Server
Truncate Seconds and Milliseconds in SQL