| |

VerySource

 Forgot password?
 Register
Search
View: 2136|Reply: 13

How does CString.Format control the number of bits?

[Copy link]

5

Threads

23

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-13 20:00:01
| Show all posts |Read mode
CString str;
float x = 123.123456;
str.Format ("% f", x);
How to control the number of digits so that the number of digits of x is within the specified range?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-17 13:00:01
| Show all posts
str.Format ("% 0.3f", x);
3 decimal places

Not tested

:)
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-17 18:36:02
| Show all posts
str.Format ("% 5.2f", x);
5 digits, 2 decimal places
Reply

Use magic Report

5

Threads

23

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-1-17 20:45:01
| Show all posts
Thank you both
By the way, how to get the number of digits of a floating-point variable with a value assigned?
Reply

Use magic Report

5

Threads

23

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-1-17 21:27:01
| Show all posts
str.Format ("% 5.2f", x);
5 digits, 2 decimal places
I don't quite understand
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-19 15:36:01
| Show all posts
5 represents how many significant digits there are, and 2 after the decimal point represents several digits after the decimal point.
Reply

Use magic Report

5

Threads

23

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-1-19 19:27:01
| Show all posts
Significant digits are the number before the decimal point plus the decimal point. I tried str.Format ("% 5.2f", x); The number of significant digits has not changed, such as x = 1456.22; after using the above formula, x Still 1456.22 and has not changed
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-21 09:54:02
| Show all posts
str.Format ("% m.nf", x);

m is the number of digits before the decimal point
n is the number of digits after the decimal point
Reply

Use magic Report

5

Threads

23

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-1-21 16:18:01
| Show all posts
Not right
If m controls the number before the decimal point, wouldn't it greatly change the original value?
Reply

Use magic Report

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-22 19:09:02
| Show all posts
This cannot be compiled in Visual studio 2005 C ++, it can be in VC6.0, what is the reason?
CString str;
float x = 123.123456;
str.Format ("% f", x);
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list