当前上下文中不存在名称 'Fix'

The name 'Fix' does not exist in the current context

在我的项目中,我正在将一些 vb.net 转换为 C#,我来到了这一行:

int thisdigit = Fix(countervalue / (Math.Pow(10, (numdigits - j - 1)))) - Fix(countervalue / (Math.Pow(10, (numdigits - j)))) * 10;

但是我得到错误:

The name 'Fix' does not exist in the current context

我该如何解决这个问题?我不明白为什么 Fix() 不存在。 但是,如果我改用 Math.Truncate(),那是行不通的,因为 thisdigit 是一个 int。 我该如何改变它?

这是我原来的 vb.net 代码:

dim dg as int
dg = Fix(value / (10 ^ (digits - j - 1))) - Fix(value / (10 ^ (digits - j))) * 10

这是我要转换的 link: https://www.developerfusion.com/code/3734/aspnet-graphical-page-hit-counter/

代码在我的 vb.net 项目中有效。我已经 运行 通过调试器转换了代码,我唯一能看到任何问题的地方就是这一行。

我也想到了这个:

double thisdigit = Math.Truncate((double)(countervalue / (10 ^ (numdigits - j - 1)))) - Math.Truncate(((double)(countervalue / (10 ^ (numdigits - j))) * 10));

我真的不明白为什么这么复杂..

原代码好像画了一个计数器,一次一位:

dim j as Integer, dg as Integer
for j = 0 to (digits-1)
   ' Extract digit from value
   dg = fix(value / (10^(digits - j - 1))) - fix(value / (10^(digits - j)))*10
   ' Add digit to the output graphic
   g.drawimage(i, New rectangle(j*dgwidth, 0, dgwidth, dgheight), New rectangle(dg*dgwidth, 0, dgwidth, dgheight), GraphicsUnit.Pixel)
   
next j

但可以肯定的是,这样做会更容易:


int pageCounter = 7234283;

string toDraw = pageCounter.ToString();

for(int i = 0; i < toDraw.Length; i++)
    someGraphics.DrawString(toDraw.Substring(i, 1), someFont, someBrush, new PointF(i * 10.0f, 0));

或者也许:


int pageCounter = 7234283;

string toDraw = pageCounter.ToString();

PointF p = new PointF(0.0f, 0.0f);

foreach(char c in toDraw){
    someGraphics.DrawString(c.ToString(), someFont, someBrush, p);
    p.X += 10.0f;
}

在 C# Discord 中 Aidy 的帮助下,我完成了这个。非常简单干净。

//Get the number of digits to display in the output graphic
//If the countervalue is 16 then "16".ToString("D5") converts it to "00016".  
//ToCharArray() turns that into an array of characters ['0', '0', '0', '1', '6']. 
//We loop through that list and convert the char back to int and we get 0, 0, 0, 1 and 6.
//Thanks to @Aidy in the C# Discord for help on this

int numdigits = Convert.ToInt32(Request.QueryString["digits"]);
var digits = countervalue.ToString("D" + numdigits.ToString()).ToCharArray();

//Create an output object
Bitmap imageoutput = new Bitmap(digitwidth * digits.Length, digitheight, PixelFormat.Format24bppRgb);  //should be 5*15 = 75 for digits.gif
Graphics graphic = Graphics.FromImage(imageoutput);  //here is our black box

//digits.gif is 150 x 20px; 
//So, if our countervalue = 16, and numdigits = 5, we want to display 00016.

for(int j = 0; j < digits.Length; j++) {
    //We loop through that digits and convert the char back to int and we get 0, 0, 0, 1 and 6.
    int thisdigitX = int.Parse(digits[j].ToString());

    //add the digit to the output graphic 
    graphic.DrawImage(digitpix, new Rectangle(j * digitwidth, 0, digitwidth, digitheight), new Rectangle(thisdigitX * digitwidth, 0, digitwidth, digitheight), GraphicsUnit.Pixel);
}

但是,我也能够获得原始代码来处理 int 和 double 的转换以及导入 Visual Basic 程序集。

using Microsoft.VisualBasic;
  int thisdigitX = Conversion.Fix(countervalue / ((int)Math.Pow(10, (double)(numdigits - j - 1)))) - ( Conversion.Fix(countervalue / ((int)Math.Pow(10,(double)(numdigits - j)))) * 10);

这是我的 github 页面的 link,如果有人真正感兴趣的话,我已经在其中发布了 entire working project。 (这里不是 self-promoting - 只是分享;我不在乎是否有人使用它)。