Processing 转换为浮点字符串,该字符串包含根据使用逗号表示小数的区域设置格式化的浮点数字

Processing 转换为浮点字符串,该字符串包含根据使用逗号表示小数的区域设置格式化的浮点数字,processing,Processing,使用我使用的“处理”编程语言,例如: float f=3.4; String s=nf(f,1,2); dest.setText(s); // the GTextField from g4p_controls thus contains "3.4" according to the locale I use. 之后,我使用: float f=float(dest.getText()); 但它不起作用,因为它没有将“,”重新定义为十进制分隔符 应该怎么做?不是bug吗?我无法复制您的问题,处理

使用我使用的“处理”编程语言,例如:

float f=3.4;
String s=nf(f,1,2);
dest.setText(s); // the GTextField from g4p_controls thus contains "3.4" according to the locale I use.
之后,我使用:

float f=float(dest.getText());
但它不起作用,因为它没有将“,”重新定义为十进制分隔符


应该怎么做?不是bug吗?

我无法复制您的问题,处理的
float()
函数似乎将“3.4”转换为
3.4

import g4p_controls.*;

GTextField dest;

void setup(){
  dest = new GTextField(this,0,0,100,30);

  float f = 3.4;
  String s = nf(f,1,2);

  println("float",f,"string",s); 

  dest.setText(s);


  float fc = float(dest.getText());

  println("string",dest.getText(),"converted float",fc,"is converted float equal to origina float?",fc == f);
}

void draw(){

}

您是否使用带逗号的区域设置“,”作为整数和小数位数的分隔符,而不是美国区域设置使用的“,”?您在我的计算机上的代码输出给出:字符串3,40转换为浮点NaN转换为浮点等于原始浮点?False问题中的示例片段没有提到
3,4
,提到了正确解析的
3.4
(但你是对的,
println(Float.parseFloat(“3,4”);
抛出了
NumberFormatException
)。您可以使用
String.format
明确使用美国语言环境:
String.format(java.util.Locale.US,“%.2f”,3.4)