Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

math - Powershell small numbers rounding up etc

How do i stop numbers being rounded up and changing in powershell for example

[int]$i = 0.0000085

showing as being 0 but i want to keep it as 0.0000085 so i can add and subtract the number etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As explained in Steven comment you are forcing the number to be an integer which by definition holds whole numbers only and that is why it's loose the precision, remove the cast for int and powershell will cast it automatically or cast to decimal instead, see an example:

$i = 0.0000085
$i
8.5E-06
$i.GetType().Name
Double

[int]$i = 0.0000085
$i
0
$i.GetType().Name
Int32

[decimal]$i = 0.0000085
$i
0.0000085

Also, if the number is not intend for any math operation, you can use string as well

$i = "0.0000085"
$i
0.0000085

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...