除去小数点后如何打印整数?

时间:2019-01-07 08:06:26

标签: java

我有一个号码

Double d = 100.000000;

我要删除小数点并将其打印为100000000

(请注意,我正在使用Java)

4 个答案:

答案 0 :(得分:3)

这是不可能的。 if ($EventID -eq '4726') { $ad_user = "$TargetUsername@avayaaws.int" $jsonbody = @{ "setupName" = "avayadev-or" "instanceName" = "000_JumpServer_DMZ" "command" = "$Command" "parameters" = @{ "mobile" = "$getMobileAttr" "ad_user"="$ad_user" "label" ="$environment" } "eventToken" = "optional" } | ConvertTo-Json #$response = Start-Job { # Invoke-RestMethod -Uri $uri -Method $verb -Body $jsonbody -Headers $header #} | wait-job | receive-job #$response = Invoke-RestMethod -Uri $uri -Method $verb -Body $jsonbody -Headers $header $response = Invoke-WebRequest -Uri $uri -Method $verb -Body $jsonbody -Headers $header $EventTrigger = $translEvent4726 Write-Output "Response is: $response" PushLogs -transaction $EventTrigger -adUser $ad_user -transResult $response } 不会在小数点后存储零,因此double等于1.0000

提示:您可以为此使用1.0。有规模。

答案 1 :(得分:1)

恐怕100.000000不等于100000000,正如@talex所述,double不会在小数点后存储零。

您最好的选择是使用String并手动删除.

String s = "100.000000";    
System.out.println(s.replaceAll("\\.", "")); //note '.' needs to be escaped

输出:

  

100000000

您可以根据需要将其解析为Double

答案 2 :(得分:0)

使用String.format格式化值并删除分隔符。

double d = 100.000;
String formatted = String.format(
                                  Locale.US,  //Using a Locale US to be sure the decimal separator is a "."
                                  "%5f",      //A decimal value with 5decimal
                                  d)          //The value to format
                         .replace(".", "");   //remove the separator
System.out.println(formatted);
  

100000000

其他示例:

  

100.000123456> 100000123

您会看到该值被截断,了解这一点很重要。

请注意,我已将字符串设置为5个十进制数字,但这取决于您。

答案 3 :(得分:0)

双精度数不会将数字存储为100.0000,而只会存储为100.0,这意味着右侧的所有不必要的零都将被删除,但是如果数字是100.01234,那么您可以使用此技巧

Double d = 100.01245;
String text = Double.toString(d);
text.replace(".","");
d = Double.parseDouble(text);

或者您可以将数字从头开始存储为

String text = "100.000000";
d.replace(".","");
double d = Double.parseDouble(text);
相关问题