dongxing4643 2016-05-30 23:16
浏览 13

php中的格式文本[关闭]

I have this code in java and i want to convert it into php. With this function i want to format some data and write them in a txt file.

Is there any classes as the text class in java to handle it? here is the code :

public static void printAll(int n1, int n2, double[][] m, 
           String[] labs, double scaling, JTextArea outext)
{
  // Some definitions for handling output formating
  NumberFormat myFormat = NumberFormat.getNumberInstance();
  FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD);
  // Following suppresses e.g. comma in 1,000 = 1000 for English locale.
  myFormat.setGroupingUsed(false);

  int w; 
  double maxval; 
  maxval = SMALL;

  for (int i =0; i < n1; i++) {
      for (int j =0; j < n2; j++) {
          // Scale up values by 'scaling' factor
          if (m[i][j] > maxval) maxval = m[i][j];
      }
  }

  String temp = myFormat.format(maxval*scaling, 
                  new StringBuffer(), fp).toString();
  // Output display field width of each number: extra 2 to 
  // account for possible minus sign, and 1 space.
  w = fp.getEndIndex() - fp.getBeginIndex() + 2;
  // System.out.println(" Output field width = " + w);

  // In the next few lines we say how we want numbers to appear.
  // max integer digits = max no. of digits before dec. point
  // max fraction digits = max no. of digits following dec. point
  // min fraction digits = min no. of digits following dec. point
  myFormat.setMaximumIntegerDigits(w);
  // We will set min and max nos. of digits following dec. point to 0
  myFormat.setMaximumFractionDigits(0);
  myFormat.setMinimumFractionDigits(0);
  for (int i=0; i<n1; i++)
  {
      // First handle cols for labels, QLT, PDS, INR:
      String myString = labs[i];
      myString = getSpaces(4 - myString.length()) + myString; 
          outext.append("|" + myString + "|");
      for (int j = 0; j < 3; j++) {
              String valString = myFormat.format(
                   scaling*m[i][j], new StringBuffer(), fp).toString();
      // With a max field width of w, we pack spaces before val.
              valString = getSpaces(4 - fp.getEndIndex()) + valString;
              outext.append(valString);
      }

      // Print each row, elements separated by spaces
          for (int j = 3; j < n2; j++)
              {
          // Scaling is in thousandths; but note if +ve only.
          w = 4;      // All cntr and corr are positive.
          if (j/3 == (double)j/3.0) {  // Here, will handle proj.
          outext.append("|");
          w = 5;                   // Allow for -ve vals.
          }
                  String valString = myFormat.format(
                      scaling*m[i][j], new StringBuffer(), fp).toString();
          // With max field width of w, we pack spaces before val.
                  valString = getSpaces(w - fp.getEndIndex()) + valString;
                  outext.append(valString);
              }
      outext.append("|");
          // Start a new line at the end of a row
      outext.append("
");
      }
}
  • 写回答

1条回答 默认 最新

  • douao8353 2016-05-30 23:25
    关注

    Yes, you can do this function using PHP. There is no built-in function that does this, so you must code it yourself

    I suggest you start breaking the function down into pieces and try using some PHP code, then if you get stuck search for that specific question before you post.

    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数