I am writing a PowerShell script that converts a number with a decimal point into an integer.
$val = 1024.24
How to convert this value to integer? (I want it to be 1024)
Use floor rounding, which rounds to the lower whole number
[Math]::Floor($val)
Edit: if just discarding decimal part is not what you are looking for you can use [Math]::Round($val) which will round the number like normal math rounding or you can use [Math]::Ceiling($val) which will round up (in your case it will round to 1025) and it is probably not what you need but it is good to know that you have these options as well.
Related
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.
How to change the number of decimal digits?
Changing the format Matlab can show only 4 (if short) or 15 (if long). But I want exactly 3 digits to show.
To elaborate on Hamataro's answer, you could also use roundn function to round to a specific decimal precision, e.g.: roundn(1.23456789,-3) will yield 1.235. However, Matlab will still display the result in either of the formats you have mentioned, i.e 1.2350 if format is set to short, and 1.235000000000000 if format is set for long.
Alternatively, if you use sprintf, you can use the %g formatting option to display only a set number of digits, regardless of where the decimal point is. sprintf('%0.3g',1.23456789) yields 1.23; sprintf('%0.3g',12.3456789) yields 12.3
You can either use sprintf or do *
var2 = round(var1*1000)/1000
I have a float that we'd like to truncate the fractional part off of but not sure the easiest way to do this. Just picking up Swift and most of the thoughts seemed way too involved. Currently I have:
details.details.append((titles[2], "\(averageAnnualSolarProduction) kW"))
but this is giving me a fraction which I want removed.
We need to combine two things:
String's format: initializer
floor operation which will make sure our decimal number doesn't round up.
If we want to keep these operations in-line, we'd want something that looks like this:
details.details.append((titles[2], String(format: "%.0f kW", floor(averageAnnualSolarProduction))))
But that's a lot of parenthesis. Looks a little better if we unnest this a bit.
let solarProduction = String(format: "%.0f kW", floor(averageAnnualSolarProduction))
details.details.append((titles[2], solarProduction))
For clarity here, floor takes a number like 3.89 and returns the largest integer number smaller than what was passed in, so it would return 3.0 here.
And to be sure that we're not printing the .0, we use String's format: initializer, which takes a format string and arguments. This works just like format strings have worked since at least C. %f specifies our argument is a floating point number, and the .0 specifies that we will display zero numbers after the decimal point.
If we're find with rounding up, we can drop the call to floor and simply use the format: initializer, but for a value of 3.89, this would give us a string that looks like "4 kW".
These two long numbers are the same except for the last digit.
test = [];
test(1) = 33777100285870080;
test(2) = 33777100285870082;
but the last digit is lost when the numbers are put in the array:
unique(test)
ans = 3.3777e+16
How can I prevent this? The numbers are ID codes and losing the last digit is screwing everything up.
Matlab uses 64-bit floating point representation by default for numbers. Those have a base-10 16-digit precision (more or less) and your numbers seem to exceed that.
Use something like uint64 to store your numbers:
> test = [uint64(33777100285870080); uint64(33777100285870082)];
> disp(test(1));
33777100285870080
> disp(test(2));
33777100285870082
This is really a rounding error, not a display error. To get the correct strings for output purposes, use int2str, because, again, num2str uses a 64-bit floating point representation, and that has rounding errors in this case.
To add more explanation to #rubenvb's solution, your values are greater than flintmax for IEEE 754 double precision floating-point, i.e, greater than 2^53. After this point not all integers can be exactly represented as doubles. See also this related question.
i have code and use double function several time to convert sym to double.to increase precision , I want to use digits function.
I want to know it is enough that I write digits in the top of code or I must write digits in above of every double function.
digits set's the precision until it is changed again. Calling digits() without any input you get the precision to verify it's set correct.
In many cases digis has absoluetly no influence on symbolic variables because an analytical solution is found. This means there are no precision errors unless you convert to double. When convertig, digits should be set to at least 16 because this matches double precision.