c正确的方法将字符串转换为可能具有前导零的十进制数(c correct method to turn a string into an number with decimal that may hav

编程入门 行业动态 更新时间:2024-10-26 11:29:46
c正确的方法将字符串转换为可能具有前导零的十进制数(c correct method to turn a string into an number with decimal that may have a leading zero)

我使用strtok , 1,2,1.0从字符串中提取了一个带小数位的1,2,1.0 。

更新此情况下的格式将限制在0.0 up to 1.0 。 所以一个主要和小数位。 printf用于用户反馈和确认,该号码被传递给请求number format nn的库。 Perhapse我误解了这条消息,而且如果数字小于1,则只需要ac节目。

我现在需要将第三个数字转换为具有nn格式的int,如果小于1,则需要前导零。

我尝试了一些int, float, double和atoi(x), strtoumax ,但在printf中得到1或1.0000....

我需要printf并在内部使用var。 那么最好的组合和格式是什么?

// third number (this may move position in token order) ????? myNumber; strcpy(string, strtok( NULL, ",")); myNumber=atoi(string); printf("the number is %?\n", myNumber); g_object_set(theItem, "alpha", myNumber, NULL); // accepts n.n only 0.0 -> 1.0

I have extracted a number with a decimal place from a string using strtok, 1,2,1.0.

UPDATE The format in this case will be limited from 0.0 up to 1.0. So a single primary & decimal place. The printf is for user feedback & confirmation, the number is passed to a library that requests a number format of n.n. Perhapse I am misinterpreting the message and as a c program only reuires .n if the number is less than one.

I now need to convert the third number to an int with a n.n format that requires a leading zero if less than one.

I have tried a few combinations of int, float, double and atoi(x), strtoumax, but get either 1 or 1.0000.... in the printf.

I need to printf and use the var internally. So what would be the best combination and format to use?

// third number (this may move position in token order) ????? myNumber; strcpy(string, strtok( NULL, ",")); myNumber=atoi(string); printf("the number is %?\n", myNumber); g_object_set(theItem, "alpha", myNumber, NULL); // accepts n.n only 0.0 -> 1.0

最满意答案

虽然你的问题不清楚,但我认为你想要的是:

double myNumber; myNumber = atof(string); // "0.5" for example printf("%03.1f", myNumber); // Output: 0.5

格式化程序意味着:

0 - 带前导零的打击垫 3 - 总输出应至少为3个字符(整数,十进制,十分之一) 如果需要,输出可能更多 .1 - 小数点后,显示1位数。

如果你真的想要一个int ,但是要显示小数位,我建议:

int myNumber = 3; printf("%03.1f", (double)myNumber); // Resulting in output: "3.0"

避免整个strtok / atoi / printf问题,只需尝试这些硬编码调用。 其中一个应该工作:

g_object_set(theItem, "alpha", "0.5", NULL); // Param is a string g_object_set(theItem, "alpha", 0.5, NULL); // Param is a double g_object_set(theItem, "alpha", .5, NULL); // Param is also a double g_object_set(theItem, "alpha", 1, NULL); // Param is an int.

告诉我们哪一个有效。

Although your question is unclear, I think what you want is:

double myNumber; myNumber = atof(string); // "0.5" for example printf("%03.1f", myNumber); // Output: 0.5

The formatter means:

0 - Pad with leading zeros 3 - Total output should be at least 3 characters (whole-number, decimal, tenths)Output may be more if needed .1 - After the decimal, show 1 digit.

If you truly want an int, but to be displayed with decimal places, I suggest:

int myNumber = 3; printf("%03.1f", (double)myNumber); // Resulting in output: "3.0"

Avoid the whole strtok / atoi / printf issues, and just try these hard-coded calls. One of them should work:

g_object_set(theItem, "alpha", "0.5", NULL); // Param is a string g_object_set(theItem, "alpha", 0.5, NULL); // Param is a double g_object_set(theItem, "alpha", .5, NULL); // Param is also a double g_object_set(theItem, "alpha", 1, NULL); // Param is an int.

Tell us which one works.

更多推荐

本文发布于:2023-04-29 10:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前导   转换为   字符串   正确   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!