--- tags: DICE C --- 6-4 輸出絕對值 === > [name=CHAWTeam] [time=Feb 17, 2022] --- 目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC) 上一篇:[6-3 發現不相等](https://hackmd.io/@CHAWTeam/DiceC-6-3) 下一篇:[6-5 預算夠不夠?](https://hackmd.io/@CHAWTeam/DiceC-6-5) --- 題目 --- ```! 輸入一個浮點數,輸出這個浮點數的絕對值,保留到小數點後兩位。 以下提供2種範例參考,你的輸出只會是其中一種: 輸入範例1: -3.14 輸出範例1: 3.14 輸入範例2: 2.71828 輸出範例2: 2.71 C++小數點位數範例: #include <iostream> #include <iomanip> using namespace std; int main(){ cout<<fixed<<setprecision(2)<<a<<endl; } ``` 程式碼 --- ### 範例1 ```c= #include <stdio.h> int main () { float a; scanf("%f", &a); if( a < 0 ) printf("%.2f", a * (-1)); else printf("%.2f", a); return 0; } ``` ### 範例2 ```c= #include <stdio.h> #include <stdlib.h> int main () { float a; scanf("%f", &a); a = abs(a); printf("%.2f\n", a); return 0; } ``` :::info `abs()`函式可以將數值轉成絕對值。 ::: 輸出 --- ```! -3.14 3.14 ``` --- [查看我們在HackMD上的所有筆記](https://hackmd.io/@CHAWTeam) 目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC) 上一篇:[6-3 發現不相等](https://hackmd.io/@CHAWTeam/DiceC-6-3) 下一篇:[6-5 預算夠不夠?](https://hackmd.io/@CHAWTeam/DiceC-6-5) --- {%hackmd Iiu5mOixR7yWkPHKCkabBg %} <iframe class="LikeCoin" height="235" src="https://button.like.co/in/embed/chawteam/button?referrer=https://hackmd.io/@CHAWTeam/DiceC-6-4" width="100%"></iframe> --- {%hackmd i1nMRrZcTFmTvoF897K9zg %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up