How To Only Show Certain Number of numbers after decimal point Double Swift - swift

I am using doubles to log hours for my app. My problem is I only want to record the number and the first number after the decimal place otherwise sometimes the number becomes like this (x.9000000000001) and I only need the x.9.
I have tried rounding the double value but it still has this weird extra amount of zeros.
Any way to only get the double to show the first number after decimal place.
Thanks

The easiest way to achieve rounding to the first decimal place is to simply do the following:
let x = 4.9000000001
let roundedX = Double(round(x * 10) / 10) // roundedX = 4.9
roundedX will be a Double representing x rounded to the first decimal. To get 2 decimal places, just multiply and divide by 100 instead of 10.

Related

Powershell Rounding Down with decimal places

I am converting some Excel Calculations to Powershell:
=ROUNDDOWN((250000.00-0)*5.00%/12,6)
This gives a value of 1041.666666
I have converted it to Powershell:
[Math]::Round((250000.00 - 0) * (5.00/100) / 12,6)
But this gives a value of 1041.666667
The problem is that the last number matters and the fact that powershell doesnt round the 7 down to 6. This causes problems down the line as the final result for the Excel is 1461.59 but for powershell it is 1461.42
So, what's the best way to get a correct round-down to a certain number of decimal places in Powershell?
Math.Round rounds to the nearest number, not down. You'd have to roll your own version which uses Floor, e.g.:
function Get-RoundedDown($d, $digits) {
$scale = [Math]::Pow(10, $digits)
[Math]::Truncate($d * $scale) / $scale
}
Get-RoundedDown (250000 * .05 / 12) 6
Note, though, that in general there is no guarantee that the number you see as a result of that actually only has six digits after the decimal point since what we're doing here is not friendly towards binary floating-point numbers. So it may well be that you receive a number like 1441.666600000003 as a result and there's little you can do about that except switching to decimal.

Prevent from doing decimals

I am summing numbers between two indices in a matrix, like this: ans = sum(my_matrix1x500(100:300));
The ans then is a number like: 351267300.4473 and so on. How do I prevent it from printing the decimals? Instead of 351267300.4473 it could print 3512673004473 or simply remove the decimal, is this possible?
Use fprintf('%.0f',X) to print X with '0' significant digits, or round(X) to remove the decimal altogether.
If you have an arbitrary number of decimal places this is not gonna work easily, since the number usually has many more decimal places than it shows. Read the discussion here.
But if you know how many decimal places you want to keep, you simply write:
p = 4 % number of decimal places to keep
ans = floor(ans * 10 ^ p);
This gives you the desired numerical value.

Understanding the datatype Double

I am trying to write a program in C to get the percent of even numbers in an array. I am thinking of writing it using int datatype. But some one mentioned to me using double will be easier. I don't understand that. Can anyone guide me with it?
What does double datatype return?
Can the return statement be given as return (double)? What will that give?
Can double convert a real number to a percent? Eg: 0.5 to 50.0
The int datatype is, as the name would suggest, integers (or whole numbers). So you cannot represent a decimal like 0.5. A double is decimal number. So you can hold numbers like 0.5. Common practice is to store your percentage as a simple decimal number like 0.5 (using the double type). Then when you need to display nicely as 50.0%, just multiply by 100 before displaying.
Here's a useful link on C's basic datatypes: http://www.tutorialspoint.com/ansi_c/c_basic_datatypes.htm

Converting a floating point number to string

I want to convert a floating point number to string.
f=0.0000
str=num2str(f,4);
This give me this-
str=
0
But I wanted to keep the result till 4 decimal points.I understand using num2str(f,4) results in precision of maximum 4 significant digits after decimal.It is not equal to 4 but maximum and that is why I get this answer.I want to know is there any way to convert this number to string with exactly 4 significant digits after decimal point?
Try using sprintf instead:
str = sprintf('%.4f', f)

Using double value as a score for GKLeaderboard?

I wondered if there's a way of using a double value on GKLeaderboard? It insists that I use int64_t, but I need double values to be used, as the leaderboard is for the fastest time, counting to six decimal points.
Since you need a decimal number down to the millions, one solution would be to simply multiply by 1,000,000 and coerce to an int64_t:
int64_t intScore = (int64_t)1000000 * doubleScore;
Then when you get the score back:
double doubleScore = (double)intScore / 1000000;
If you're measuring with microsecond precision, you can just have the units be microseconds instead of seconds. Then you can use integers.
(Similarly, money should never be represented as a floating-point value, but as an integral value representing the smallest amount of money you want to consider — so a dollar would be 100 (cents) rather than 1.0 (dollars).)