是否可以使用Convert.ToInt32(double)并使其选择最小值?
我已经阅读了msdn中的示例,它使用最接近的值将double转换为int,这意味着如果我有一个等于2.9的double,它会将int设置为3.
是否可以使用convert.toint32并使用2?
我有两个日期作为整数.如何在c#中找到这两个整数之间的月份差异?
例如:
Int32 beginDate= 20130307(yyyymmdd)
Int32 beginDate= 20140507(yyyymmdd)
Run Code Online (Sandbox Code Playgroud)
我需要14个月的结果.
我已经尝试过了:
DateTime beginDatepar = Convert.ToDateTime(beginDate);
DateTime endDatepar = Convert.ToDateTime(beginDate);
int monthDifference = ((beginDatepar.Year - endDatepar.Year) * 12) +
beginDatepar.Month - endDatepar.Month;
Run Code Online (Sandbox Code Playgroud)
但是当我将Int32转换为Datetime时,错误是"从'Int32'到'DateTime'的无效转换"
我在一段相当简单的代码上遇到了一个奇怪的问题.代码的相关部分如下:
void foo(int32 in_sd_id, int32 out_sd_id)
{
int32 nsds; /* number of data sets in the file */
int32 nattr; /* number of global attributes in the file */
int32 attr_cnt; /* count of number of attribute */
int32 attr_index; /* attribute index */
int32 attr_type, attr_size; /* attribute type and size */
char attr_name[40];
ret = SDfileinfo(in_sd_id, &nsds, &nattr);
printf("nattr is %d\n", nattr);
/* test to see if num_datasets and num_global_attr can be retrieved from in_sd_id */
if (ret …
Run Code Online (Sandbox Code Playgroud) 我想问一下如何重新排序数字,Int32
这样就可以得到最大的数字.这是一个可视化我想要做的事情的例子:
2927466 -> 9766422
12492771 -> 97742211
Run Code Online (Sandbox Code Playgroud)
我想在不使用System.Linq
命名空间的情况下执行数字的排序,而不将整数转换为字符串值.这是我到目前为止所得到的:
public static int ReorderInt32Digits(int v)
{
int n = Math.Abs(v);
int l = ((int)Math.Log10(n > 0 ? n : 1)) + 1;
int[] d = new int[l];
for (int i = 0; i < l; i++)
{
d[(l - i) - 1] = n % 10;
n /= 10;
}
if (v < 0)
d[0] *= -1;
Array.Sort(d);
Array.Reverse(d);
int h = 0;
for (int i = 0; …
Run Code Online (Sandbox Code Playgroud) 看来PowerShell hashtable(@{}
)默认是string→string的映射.但是我希望我的价值类型Int32
能让我对它进行计算.
在声明哈希表变量时如何指定类型信息?
使用Swift 3并且在IF语句中比较Int32和Int时遇到问题.
// myIntFromCoreData is an Int32
// kMyConstant is an Int
if myIntFromCoreData == kMyConstant // error: Binary operator "==" cannot be applied to operands of type 'Int32' and 'Int'
{
Log("Cool")
}
Run Code Online (Sandbox Code Playgroud)
所以,我尝试使用以下方法将值转换为Int32或Int:
myint32.intValue // Value of type 'Int32' has no member 'intValue'
myint32 as Int // Cannot convert value of type 'Int32' to type 'Int' in coercion
kMyConstant as Int32 // Cannot convert value of type 'Int' to type 'Int32' in coercion
Run Code Online (Sandbox Code Playgroud)
但不断得到上述错误.任何人都可以提供一些方向,如何处理这个?
基本上,我想要的是在(最右边的 32 位)一个无符号的 64 位 int 中“存储”一个有符号的 32 位 int - 因为我想将最左边的 32 位用于其他目的。
我现在正在做的是一个简单的演员和面具:
#define packInt32(X) ((uint64_t)X | INT_MASK)
Run Code Online (Sandbox Code Playgroud)
但是这种方法有一个明显的问题:如果X
是一个正整数(第一位没有设置),一切都会好起来的。如果它是负数,它就会变得一团糟。
问题是:
如何以最快和最有效的方式实现上述目标,同时支持负数?
在 64 位平台上,Int
与 的大小相同Int64
,在 32 位平台上,Int
与Int32
.
可以更改此行为吗,即可以Int
强制Int32
64 位平台上的大小吗?