我正在编写一个 csv 转换器来将交易的文本文档转换为 csv 文档,但我遇到了一个小问题..
foreach (var transaction in transactions)
{
output.Append(
string.Format("{0:dd/MM/yy},{1},{2},{3},{4:0.##},{5}",
transaction.Date,
transaction.Payee,
transaction.Category,
transaction.Memo,
transaction.Outflow,
transaction.Inflow));
output.AppendLine();
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好,我遇到的小问题是该Outflow属性是一个浮点数,并且我的语言环境使用逗号作为十进制分隔符,这在CSV 中显然是一个很大的问题,所以不是让我们说 10.50 它会输出 10,50,有什么简单的方法可以解决这个问题吗?
我正在编写一个具有选项菜单的应用程序,其中包含三个语言选项:自动、英语、希腊语。
当用户更改设置时,我将其保存在共享首选项中,并使用新任务标志调用我的开始活动
当该活动开始时(甚至在 setContentView 之前)我调用以下方法:
string language = MainApplication.Language; //this is from shared preferences
if (language == null || language.Equals("auto")) {
language = Java.Util.Locale.Default.Language; //if language is not set, or the setting is auto then load the default language
MainApplication.Language = "auto"; //save into the shared preferences that the language was set to auto
}
App.instance.set_language(language); //update the data layer's language
Java.Util.Locale locale = new Java.Util.Locale(language);
Java.Util.Locale.Default = locale; //change the default locale
Configuration config = new Configuration();//create a new …Run Code Online (Sandbox Code Playgroud) 我需要将欧洲大陆格式的字符串货币字符串转换为浮点数:
输入:
'6.150.593,22 €'
Run Code Online (Sandbox Code Playgroud)
意识到小数点是逗号,千位分隔符是句点字符。
输出:
6150593.22
Run Code Online (Sandbox Code Playgroud)
我会阅读这些问题,但它们仅适用于美元货币和语言环境:
currency_euros='6.150.593,22 €'
float(currency_euros[:-2])
Run Code Online (Sandbox Code Playgroud)
'6.150.593,22 €'
Run Code Online (Sandbox Code Playgroud)
更新:按照@IrmendeJong 的回答:
>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, "es")
'es'
>>> print(locale.currency(6150593.22))
6150593,22 €
>>> money = '6.150.593,22 €'
>>> locale.atof(money)
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
locale.atof(money)
File "C:\Python35\lib\locale.py", line 318, in atof
return func(delocalize(string))
ValueError: could not convert string to float: '6150593.22 €'
>>>
Run Code Online (Sandbox Code Playgroud)
我很高兴这locale.currency()工作正常,但它的互惠方法locale.atof()不起作用。
我想在 cronjob 调度程序中将语言环境设置为默认语言以外的其他语言。 https://github.com/ghaiklor/sails-hook-cron
cronjob 调度程序代码如下所示:
// ['seconds', 'minutes', 'hours', 'dayOfMonth', 'month', 'dayOfWeek']
module.exports.cron = {
job: {
schedule: '0 0 12 * * *',
onTick: function() {
SomeService.sendSms()
},
timezone: 'Asia/Jerusalem'
}
}
Run Code Online (Sandbox Code Playgroud)
但是我无法设置语言环境,因为它不是控制器而是服务,而且我无法全局访问 req.setLocale。
当我通过请求参数更改首选语言时,网页中的语言会发生变化(使用 检索消息<spring:message code="xxxx"/>)但控制器中的区域设置不会更改,例如:
private void simpleControllerMethod(HttpServletRequest request, HttpServletResponse response, ModelAndView model) {
System.out.println(request.getLocale().toString()); // prints default application locale no matter what
}
Run Code Online (Sandbox Code Playgroud)
dispather-servlet.xml 包含:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
<property name="defaultEncoding" value="UTF-8" />
<property name="fallbackToSystemLocale" value="false" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="lt" />
<property name="cookieName" value="lang" />
<property name="cookieMaxAge" value="3600"/>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
</mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)
为什么 HttpServletRequest 的语言环境不会改变?
我正在设置语言环境以进行翻译。当语言环境在会话期间保持不变时,它会起作用。但是,如果我在会话中间更改语言环境并重新加载页面,它会保留在旧语言环境中。
您知道从浏览器获取最新语言设置的方法吗?
我的代码:
@SpringUI
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = MainUI.class)
public class MainUI extends UI
{
@Override
protected void init(VaadinRequest request)
{
log.debug("MainUI init! locale: {}", getLocale());//or getSession().getLocale()
messageByLocaleService.setLocale(getLocale());
...
Run Code Online (Sandbox Code Playgroud)
我自己找到了一个解决方案,但我不能 100% 确定这是正确的解决方案。
我的要求是根据国家/地区代码(DE、IT、ES、IE、PT、UK)获取当前日期和时间。有任何可用的Java API吗?
我也尝试设置指定国家/地区的语言环境,但得到相同的输出?
有人能告诉我为什么即使我也在设置语言环境,我也会得到“亚洲/加尔各答”时区吗?
public void timezone() {
final Locale spanishLocale = new Locale("es", "ES");
final Locale ukLocale = new Locale("en", "UK");
final Locale deLocale = new Locale("de", "DE");
final Locale itLocale = new Locale("it", "IT");
final Locale ptLocale = new Locale("pt", "PT");
final Locale ieLocale = new Locale("es", "ES");
final Calendar cal = Calendar.getInstance(spanishLocale);
System.out.println("date and time in spain is " + cal.getTime());
System.out.println(
"date and time in uk is " + Calendar.getInstance(ukLocale).getTime() + " :: " + Calendar.getInstance(ukLocale).getTimeZone()); …Run Code Online (Sandbox Code Playgroud) 如何获取给定货币字符串(“GBP”、“USD”等)的货币符号?我想出的最好的似乎冗长可笑,还有其他方法还是我最好使用查找表?
const userLocale = "EN-gb";
const userCurrency = "GBP";
const withValueEl = document.querySelector(".withValue");
const withoutValueEl = document.querySelector(".withoutValue");
const valueWithCurrency = Number(0).toLocaleString(userLocale, {
style: "currency",
currency: userCurrency,
minimumFractionDigits: 2
});
const valueWithoutCurrency = valueWithCurrency.replace(Number(0).toLocaleString(userLocale, {
minimumFractionDigits: 2
}), "")
withValueEl.innerHTML = valueWithCurrency
withoutValueEl.innerHTML = valueWithoutCurrencyRun Code Online (Sandbox Code Playgroud)
With value:<span class="withValue"></span><br /> Without value:<span class="withoutValue"></span><br />Run Code Online (Sandbox Code Playgroud)
我在练习中遇到了一些严重的困难,我将编写一个简单的程序,该程序将提供工作日名称(不是 int 值)作为输出。更重要的是,它应该是从当前日期起 90 天之后的工作日,并且应该以当地语言(丹麦语)显示。
我是初学者,正在寻求有关如何执行此操作的提示。
练习的重要部分是我不能使用比导入的更多的类(见下面的代码)。
我自己发现的是如何获取当前日期并将其显示为工作日(我知道它从星期日开始从 1 开始计数),但我不知道如何强制它将此整数显示为本地语言的工作日名称.
package days01;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.Locale;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;
public class Dates {
public static void main(String[] argv) {
Calendar one = new GregorianCalendar();
int day = one.get(Calendar.DAY_OF_WEEK);
System.out.println(day);
}
}
Run Code Online (Sandbox Code Playgroud)
预期的输出应该是从当前日期开始的单个工作日名称 90。